Simplify Repetitive Tasks with Loops Flashcards

3
Q

What of the following is a good metaphor for a loop?

A

A loop is like a track. Each time around the track is like one time through the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When should you use loops?

A

When you want to repeat code over and over a certain number of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Finish the code below to create a while loop:

var counter = 0;

_____ ( counter < 10 ) {

console.log(counter);

counter += 10;

}

A

while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Finish the code below so that the loop runs 10 times:

var counter = 0;

while ( counter < _____ ) {

console.log(counter);

counter += 1;

}

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why will this loop never run :

var num = 0;

while (num > 20 ) {

alert(num);

num += 1;

}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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;
}

A

counter < 11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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;

A

while(count < 26) {
document.write(count);
count += 1;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.”);

A

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.”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Given the code below, what will print out to the console?

while (false) {

console.log(‘Hello’);

}

console.log(‘Goodbye’);

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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 <= _____ )

A

9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Given the code below, what will print out to the console?

do {

console.log(‘Hello’);

} while (false);

console.log(‘Goodbye’);

A

Hello

Goodbye

Even though the condition is false, a do..while loop always run at least once.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Finish this code to create a do…while loop.

var counter = 0;

_____ {

console.log(counter);

counter += 1;

} _____ (counter < 10);

A

do, while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.");
A

var secret;

do {
secret = prompt(“What is the secret password?”);
} while(secret !== “sesame”)

document.write(“You know the secret password. Welcome.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.

A
for(var i = 4; i \<= 156; i += 1) {
  console.log(i);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What statement will make the JavaScript interpreter exit a loop even when the loop condition is still true?

A

break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How many times will the following loop run:

for (var j = 0; j <= 100; j += 10) {

console.log( j );

}

A

11

19
Q

Complete the code below to create a for loop that executes 10 times, writing the value of the variable i to the document each time:

_____ (var i = 0; i < _____ ; i++) {

document.write(i);

}

A

for, 10

20
Q

The code below logs all of the even numbers from 2 to 24 to the JavaScript console. However, there’s a lot of redundant code here. Re-write this using a loop.

console. log(2);
console. log(4);
console. log(6);
console. log(8);
console. log(10);
console. log(12);
console. log(14);
console. log(16);
console. log(18);
console. log(20);
console. log(22);
console. log(24);

A
for(var i = 2; i \<= 24; i += 2) {
  console.log(i);
}