for loop in function javascript code example

Example 1: javascript for loop

var colors=["red","blue","green"];
for (let i = 0; i < colors.length; i++) { 
  console.log(colors[i]);
}

Example 2: javascript loop through array

var myStringArray = ["Hello","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
    console.log(myStringArray[i]);
    //Do something
}

Example 3: javascript loop if statement

// a loop is where you say i = 5 and if i < 6 {
 // print("hdyugvryvg");
// it means i = 5 and 6 is greater than 5 so it should print out hdyugvryvg 5 times
//}

// example

for (let i = 0; i < 10000; i++) {
 	console.log('huduiduin 100000 times'); 
}



// dont try this put a lower number this nearly crashed my browser

Example 4: javascript for loop 5 times

for (let step = 0; step < 5; step++) {
  // Runs 5 times, with values of step 0 through 4.
  console.log('Walking east one step');
}

Example 5: for in loop javascript

function diagonalDifference(arr) {
    // Write your code here
    // return (arr); 11,2,4,4,5,6,10,8,-12
    let n = arr.length;
    let pri,sec = 0;

    pri = sec = 0; //initialize both diagonal sum to 0
    for (let i= 0; i < n; i++) {
        //console.log( "PRI(arr)= ", i,i);
        //console.log( arr[i][i]);
        //console.log( "SEC(arr)= ", i,n-i-1);
        //console.log( arr[i][n-i-1]);
        pri += arr[i][i];
        sec += arr[i][n - i - 1]; 
    }
	
  	// return |x| is the absolute value of x
    if(pri==sec){
        return (0);
    }else {
        if (pri>sec){
            return (pri-sec);
        }else{
            return (sec-pri);
        }
    }
}