In: Computer Science
/* Assignment : Complete this javascript file according instructions in comments. */ // 1) Create a for loop that loops through its code block 10 times // In the first statement, set the variable i equal to zero // In the second statement, tell the loop to execute while i is less than ten // In the third statement, increment i by one each time the loop executes // In the code block of the loop, print the variable i to the console // 2) Create an array named foods and put your 5 favorite foods // in the array as items. // 5 fav foods = sushi, ramen, korean bbq, pizza, wings // Create a for / in loop that will loop through the items in the foods array // Print a favorite food item to the console each time the loop executes // When finished, you should have printed each favorite food item to the console // 3) Create a variable x and set it equal to 50. // Create a while loop that executes while x is greater than zero. // Print the value of x to the console every time the loop executes. // Decrement the value of x by one every time the loop executes. // Your loop should count backwards from 50 to 1 and print each number in the // console. // 4) Create a variable named myNum and set it equal to 10. // Create a while loop that executes while myNum is less than 5. // In the loop, print the value of myNum to the console. // Below that loop, create a do / while loop. // Set the while condition the same as first while loop. // Print the value of myNum to the console in the loop. // Comment below your loops to explain the outcome. /* multi-line comment */ // 5) Create a variable named x and set it equal to zero. // Create a while loop that executes while x is less than ten. // In the code block of the loop, set x equal to a random number between // one and ten using the previous random number formula we learned with // the Math object. // After assigning x the random number, print the value of x to the console. // The loop should continue to execute until the value of x equals 10.
/* Assignment :
Complete this javascript file according instructions
in comments. */
// 1) Create a for loop that loops through its code block 10 times
// In the first statement, set the variable i equal to zero
// In the second statement, tell the loop to execute while i is less than ten
// In the third statement, increment i by one each time the loop executes
// In the code block of the loop, print the variable i to the console
var i = 0;
while (i < 10) {
console.log(i);
i++;
}
// 2) Create an array named foods and put your 5 favorite foods
// in the array as items.
// 5 fav foods = sushi, ramen, korean bbq, pizza, wings
// Create a for / in loop that will loop through the items in the foods array
// Print a favorite food item to the console each time the loop executes
// When finished, you should have printed each favorite food item to the console
var foods = ['sushi', 'ramen', 'korean bbq', 'pizza', 'wings'];
for (var food in foods) {
console.log(food);
}
// 3) Create a variable x and set it equal to 50.
// Create a while loop that executes while x is greater than zero.
// Print the value of x to the console every time the loop executes.
// Decrement the value of x by one every time the loop executes.
// Your loop should count backwards from 50 to 1 and print each number in the
// console.
var x = 50;
while (x > 0) {
console.log(x);
c--;
}
// 4) Create a variable named myNum and set it equal to 10.
// Create a while loop that executes while myNum is less than 5.
// In the loop, print the value of myNum to the console.
// Below that loop, create a do / while loop.
// Set the while condition the same as first while loop.
// Print the value of myNum to the console in the loop.
// Comment below your loops to explain the outcome. /* multi-line comment */
var myNum = 10;
while (myNum < 5) {
console.log(myNum);
}
do {
console.log(myNum);
} while (myNum < 5);
/*
This while code does not execute.
but the do-while loop execute once, because the condition check was done after it goes once through the loop.
*/
// 5) Create a variable named x and set it equal to zero.
// Create a while loop that executes while x is less than ten.
// In the code block of the loop, set x equal to a random number between
// one and ten using the previous random number formula we learned with
// the Math object.
// After assigning x the random number, print the value of x to the console.
// The loop should continue to execute until the value of x equals 10.
var x = 0;
while (x < 10) {
x = Math.floor(Math.random() * 11);
console.log(x);
}