Skip to content
Advertisement

Trying to increment an integer in an Array

Good Evening, I am trying to increment an integer that I have index position of ‘0’ in my array, each time my function gets called. I have the variable added with .push, but then I just want to add one to that. I am trying to use indexof(), I have also tried findIndex(). Below is my code

JavaScript

Advertisement

Answer

Following the re-wording of the issue with your comment:

[…] the objective is to take the number ‘0’ that’s in the 0th position of the array and increment it by 1 each time the function runs

The first issue I see is that you may be misusing the indexOf function. That will not give you the index of an array, but instead the position of a particular value of an array.

Example:

JavaScript

So, for you to access the element in the 0th position, you will want to do arr[0]. So in your code you will want to do the following:

JavaScript

Now… This will have a second problem which is your usage of the incrementation operator ++. Although this will increment the flow_complete variable, it does not return it to set storage_array[0] as you intend to do so.

To fix this, all you have to do is increment flow_complete prior to assigning it to storage_array[0]. It will look something like this:

JavaScript

However, if my interpretation of your comment above is correct, there is one more problem, which you are assigning flow_complete to storage_array[0] each time the function runs. flow_complete is set to 0 as you can see in your own block of code within the scope of your addFunction, so this means it will always go back to 0 every time it is run.

Going back to your original comment, you want to increment the value in the 0th index of storage_array, not flow_complete itself, correct? If this is the case, you can completely get rid of the flow_complete variable and instead increment storage_array[0]. This will make your if-block look as follows:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement