简体   繁体   中英

how do we increasing variable count using a foreach loop in JavaScript

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

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);
}

This is syntax for forEach array, used on arrays:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM