for loops: flags, Booleans, array length, and loopus interruptus Flashcards

1
Q

In one word, what is a flag?

A

variable

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

What is the keyword that stops a loop from continuing to loop?

A

break

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

There are two Booleans. Name either one of them.

A

true
or
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
The statement assigns the number of elements in the array to the variable. Fill in the blank.
var num = generals.\_\_\_\_\_\_\_\_;
A

length

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

Find the number of elements in the array cities and assign the number to the variable numCities, which hasn’t been declared beforehand.

A
The correct code is:
var numCities = cities.length;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code two lines. The first line reverses the flag x, which was originally set to the positive Boolean. The second line stops the looping.

A

The correct code:
x = false;
break;

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

Set a flag with an initial Boolean value of your choice.

A

Sorry, that’s incorrect. An example:
var matchFound = false;
or
var noMatch = true;

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

Find the number of elements in an array and assign the number to a variable, which hasn’t been declared beforehand.

A

var elements = songbirds.length;

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

Code the first line of a for loop with the usual counter, the usual starting value, and the usual incrementing. Limit the number of loops by the number of elements in the array pets.

A

for (var i = 0; i < pets.length; i++) {

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

In 3 statements, assign a number to a variable that’s already been declared, assign a Boolean (either one) to a flag that’s already been declared, and interrupt a loop. Don’t indent any of the lines.

A

Sorry, that’s incorrect. An example:
x = 1;
found = true; (or false;)
break;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Complete this code to display an alert if a match isn't found.
var matchFound = false;
for (var i = 0; i < list.length; i++) {
  if (userInput === list[i]) {
    alert("Match found");
    matchFound = true;
    break;
  }
A

if (matchFound === false) {
alert(“Match not found”);
}

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