0

I am trying to run a foreach loop to keep looping into the var number is reached. I also need to keep track of the index. so far what I have tried

const data = ()=>{
    const myNumber = input.getPortCount() // Example currently this will be output a number 0 - 9
    foreach(i == 0, i => myNumber ++i){
         return console.log('i equals', i)
    }
}

I need I to increase by 1 start at 0 into it equal to the number of the variable myNumber

1
  • It seems like you are trying to do a for loop! for (var i = 0; i < myNumber; i++) { console.log('i equals', i) } forEach are for array looping Commented Feb 28, 2021 at 19:39

2 Answers 2

1

You should go learn about loops. You need here the for loop and not foreach.... but you have more mistakes. here is the loop you need:

for (var i = 0; i <= myNumber; ++i){
     console.log('i equals', i);
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is syntax for forEach array, used on arrays :

array.forEach(function(currentValue, index, arr), thisValue)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.