0
How to add formula to JS loop for?
I have to use formula in the for loop. Instead of increment i++. How it should be correct?
7 Antworten
+ 10
Nadia Den You can use forEach() loop
+ 6
// You can modify i in the body of the loop, example:
for (let i = 0; i < 10; ) {
console.log(i);
i += 2;
}
// If you need help with your own code, please link your code.
+ 5
Nadia Den I didn't understand. Do you want to output all values of 2a - 1, with a between 2 and 10000? If so, you don't even need to change the for loop specs.
Could you pls ellaborate on your task?
+ 4
Depending on the formula, you can just use it in place of i++
+ 2
Thanks for reply, guys. I mean formula 2a - 1. Where let a = 2, a <= 10000. Formula of all numbers between this.
+ 1
It was mistake because of 2a, must be 2*a - 1
0
Nadia Den
you have already answered your own question.
One small suggestion is to store the results in an array. It's easier than calling console.log 10000 times.
let res = [];
for(let a=2 ; a<=10000; a++)
res.push(2*a - 1);
console.log(res);