FCC Loops
by vanduzled
JavaScript
// Setup
var myArray = [];
var i = 5;
while( i >= 0 ){
myArray.push(i);
i--;
}
// Only change code below this line
//[ 5, 4, 3, 2, 1, 0 ]
// Iterate Odd Numbers With a For Loop
var myArray = [];
for( var i = 1; i <= 5; i++ ){
myArray.push(i);
}
// Only change code below this line
console.log(myArray);
//[ 1, 2, 3, 4, 5 ]
// Setup
var myArray = [];
for(var i = 1; i <= 9; i += 2 ){
myArray.push(i);
}
// [ 1, 3, 5, 7, 9 ]
// Count Backwards With a For Loop
var myArray = [];
for(var i = 9; i > 0; i -= 2){
myArray.push(i);
}
//[ 9, 7, 5, 3, 1 ]
// Only change code below this line
// Iterate Through an Array with a For Loop
var myArr = [ 2, 3, 4, 5, 6];
var total = 0;
for( var i = 0; i < myArr.length; i++ ){
total = myArr[i] + total;
}
// console.log(total); 20
//Nesting For Loops
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for( var i=0; i < arr.length; i++ ){
for( var j=0; j < arr[i].length; j++ ){
product *= arr[i][j];
}
}
// Only change code above this line
return product;
}
multiplyAll([[1,2],[3,4],[5,6,7]]);
//console.log(product); 5040
// Iterate with JavaScript Do...While Loops
var myArray = [];
var i = 10;
// Only change code below this line
do{
myArray.push(i)
i++
}while (i < 5);