How to add formula to JS loop for? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

26th Apr 2023, 12:08 PM
Nadia Den
Nadia Den - avatar
7 Answers
+ 10
Nadia Den You can use forEach() loop
26th Apr 2023, 12:10 PM
Sakshi
Sakshi - avatar
+ 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.
26th Apr 2023, 1:21 PM
Lisa
Lisa - avatar
+ 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?
26th Apr 2023, 10:23 PM
Emerson Prado
Emerson Prado - avatar
+ 4
Depending on the formula, you can just use it in place of i++
26th Apr 2023, 2:36 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Thanks for reply, guys. I mean formula 2a - 1. Where let a = 2, a <= 10000. Formula of all numbers between this.
26th Apr 2023, 8:06 PM
Nadia Den
Nadia Den - avatar
+ 1
It was mistake because of 2a, must be 2*a - 1
1st May 2023, 11:27 AM
Nadia Den
Nadia Den - avatar
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);
27th Apr 2023, 1:13 PM
Bob_Li
Bob_Li - avatar