loop methods js code example
Example 1: js for loop
var i;
for (i = 0; i < 5; i++) {
console.log("The Number Is: " + i);
};
console.log("The Number Is: " + 0);
console.log("The Number Is: " + 1);
console.log("The Number Is: " + 2);
console.log("The Number Is: " + 3);
console.log("The Number Is: " + 4);
Example 2: how to make a javascript for loop
var tops = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144];
for (let i = 0; i < tops.length; i++) {
document.write(tops[i] + "tops");
}
Example 3: for in loop javascript
function diagonalDifference(arr) {
let n = arr.length;
let pri,sec = 0;
pri = sec = 0;
for (let i= 0; i < n; i++) {
pri += arr[i][i];
sec += arr[i][n - i - 1];
}
if(pri==sec){
return (0);
}else {
if (pri>sec){
return (pri-sec);
}else{
return (sec-pri);
}
}
}