Simplify Repetitive Tasks with Loops Flashcards
What of the following is a good metaphor for a loop?
A loop is like a track. Each time around the track is like one time through the loop.
When should you use loops?
When you want to repeat code over and over a certain number of times.
Finish the code below to create a while
loop:
var counter = 0;
_____ ( counter < 10 ) {
console.log(counter);
counter += 10;
}
while
Finish the code below so that the loop runs 10 times:
var counter = 0;
while ( counter < _____ ) {
console.log(counter);
counter += 1;
}
10
Why will this loop never run :
var num = 0;
while (num > 20 ) {
alert(num);
num += 1;
}
The variable num begins with the value of 0. The condition asks if that variable is greater than 20. I isn’t greater than 20 so the loop never runs.
This is a nearly complete while loop, but something is missing. The loop should run 10 times, but it’s not working at all. Can you fix it?
var counter = 1; while ( \_\_\_\_\_ ) { document.write("
Now in loop #” + counter + “
”);
counter += 1;
}
counter < 11
In this challenge, you will create a while loop that prints to the document 26 times. We’ve added a variable named count, use it to track the number of times the loop runs. Don’t forget to use the document.write() method inside the loop.
var count = 0;
while(count < 26) {
document.write(count);
count += 1;
}
The code in app.js just opens a prompt, asks for a password, and writes to the document. Something is missing. Add a while loop, so that a prompt keeps appearing until the user types “sesame” into the prompt.
var secret = prompt(“What is the secret password?”);
document.write(“You know the secret password. Welcome.”);
var secret = prompt(“What is the secret password?”);
while(secret !== “sesame”) {
secret = prompt(“What is the secret password?”);
}
document.write(“You know the secret password. Welcome.”);
Given the code below, what will print out to the console?
while (false) {
console.log(‘Hello’);
}
console.log(‘Goodbye’);
Goodbye
Because the condition for the while loop is false the loop never runs and ‘Hello’ never prints, but the second console.log does run because it’s outside the loop.
Finish the code in this loop’s condition, so that ‘I love JavaScript’ is printed to the console 10 times:
var x = 0;
do {
console.log(‘I love JavaScript’);
x += 1;
} while ( x <= _____ )
9
Given the code below, what will print out to the console?
do {
console.log(‘Hello’);
} while (false);
console.log(‘Goodbye’);
Hello
Goodbye
Even though the condition is false, a do..while loop always run at least once.
Finish this code to create a do…while loop.
var counter = 0;
_____ {
console.log(counter);
counter += 1;
} _____ (counter < 10);
do, while
Below is the code from the last code challenge. This would work better as a do…while loop. Re-write the code using a do…while loop.
var secret = prompt("What is the secret password?"); while ( secret !== "sesame" ) { secret = prompt("What is the secret password?"); } document.write("You know the secret password. Welcome.");
var secret;
do {
secret = prompt(“What is the secret password?”);
} while(secret !== “sesame”)
document.write(“You know the secret password. Welcome.”)
Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) method.
for(var i = 4; i \<= 156; i += 1) { console.log(i); }
What statement will make the JavaScript interpreter exit a loop even when the loop condition is still true?
break;