+ 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.
1 Respuesta
+ 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/>" );
}