What things can we do with for loop? JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What things can we do with for loop? JS

What exactly is the use code for loop? Can u meantion some things? Example: Switch: We can display a different text on different days (Mom, Tue, Wed...). I don't really know things we can do with for loop besides print numbers from num to num.

8th Jul 2019, 8:20 PM
Ginfio
Ginfio - avatar
1 Answer
+ 42
For loops are very useful for working with arrays, especially when you don't know their length. Arrays can be created by users entering their data into your program. It's always hard to predict how many elements will be in arrays in those situations. However arrays have a "length" property and you can use it in for loops. You can also do arithmetics inside of for loops and print the results. In this example you can count money spent on a faraway vacation: var myExpences []; //array containing all items you bought abroad var exchangeRate; for (x = 0; x < myExpences.length; x++) { myExpences[x] = myExpences[x] * exchangeRate; document.write(myExpences[x] + "<br/>" ); }
10th Jul 2019, 9:05 AM
🇺🇦 Vitya 🇺🇦
🇺🇦 Vitya 🇺🇦 - avatar