for loops: flags, Booleans, array length, and loopus interruptus Flashcards
In one word, what is a flag?
variable
What is the keyword that stops a loop from continuing to loop?
break
There are two Booleans. Name either one of them.
true
or
false
The statement assigns the number of elements in the array to the variable. Fill in the blank. var num = generals.\_\_\_\_\_\_\_\_;
length
Find the number of elements in the array cities and assign the number to the variable numCities, which hasn’t been declared beforehand.
The correct code is: var numCities = cities.length;
Code two lines. The first line reverses the flag x, which was originally set to the positive Boolean. The second line stops the looping.
The correct code:
x = false;
break;
Set a flag with an initial Boolean value of your choice.
Sorry, that’s incorrect. An example:
var matchFound = false;
or
var noMatch = true;
Find the number of elements in an array and assign the number to a variable, which hasn’t been declared beforehand.
var elements = songbirds.length;
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.
for (var i = 0; i < pets.length; i++) {
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.
Sorry, that’s incorrect. An example:
x = 1;
found = true; (or false;)
break;
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; }
if (matchFound === false) {
alert(“Match not found”);
}