Loops Flashcards
for - loops
for - loops through a block of code a number of times
for/in - loops
for/in - loops through the properties of an object
while - loops
while - loops through a block of code while a specified condition is true
do/while - loops
do/while - also loops through a block of code while a specified condition is true
Review: Loops
for loops allow us to repeat a block of code a known amount of times.
We can use a for loop inside another for loop to compare two lists.
while loops are for looping over a code block an unknown amount of times.
Infinite loops occur when stop conditions are never met.
Nested For Loop syntax
for (let i = 0; i < myArray.length; i++) { for (let j = 0; j < yourArray.length; j++) { //Code To Run } }
While Loop syntax
while (condition) { // code block that loops until condition is false }
While Loop example
let cards = [‘Diamond’, ‘Spade’, ‘Heart’, ‘Club’];
let currentCard = ‘Heart’;
while(currentCard !== ‘Spade’) {
console.log(currentCard);
currentCard = cards[Math.floor(Math.random() * 4)];
}
console.log(‘Spade’);
For Loop example
let thingsToDo = [‘do laundry’, ‘take out the garbage’, ‘make dinner’, ‘walk the dog’, ‘go to the bank’];
for (let thingsToDoIndex = thingsToDo.length - 1; thingsToDoIndex >= 0; thingsToDoIndex--) { console.log('I need to ' + thingsToDo[thingsToDoIndex] + '.'); }
Which statement is true about while loops?
Which statement is true about while
loops?
while loops run an infinite amount of times as long as their condition remains true.
while loops only run while their condition is false.
while loops always loop over a code block a known amount of times.
while loops will run at least once, then will run again if their condition is true.
while loops run an infinite amount of times as long as their condition remains true.
What is the stop condition of this loop?
for (let i = 0; i < 10; i++) {
console.log(‘Loop: ‘ + i);
}
What is the stop condition of this loop?
console.log(‘Loop: ‘ + i);
i < 10
i++
let i = 0
i < 10
What will be the result of the code below?
let animals = [‘Grizzly bear’, ‘Sloth’, ‘Sea lion’];
for (let animalsIndex = 0; animalsIndex < animals.length; animalsIndex++) {
console.log(animals[animalsIndex]);
}
What will be the result of the code below?
‘Sea Lion’
‘Grizzly Bear’
‘Sloth’
‘0: Grizzly Bear’
‘1: Sloth’
‘1: Sea Lion’
‘Grizzly Bear’
‘Sloth’
‘Sea Lion’
animals[0]
animals[1]
animals[2]
‘Grizzly Bear’
‘Sloth’
‘Sea Lion’
What is a good use for nested for loops?
What is a good use for nested for
loops?
To compare two lists.
Nested loops are bad - they run forever.
Nested loops allow us to double check our if/else statements.
Nested loops run the same code twice.
To compare two lists.
Which statement is true about for loops?
Which statement is true about for
loops?
for loops always count from 0 upwards.
for loops run an unknown amount of times.
for loops never result in infinite loops because their stop condition is set from the beginning.
for loops always loop over a code block a known amount of times.
for loops always loop over a code block a known amount of times.
What problem is caused by the code below?
for (let i = 10; i > 0; i++) {
console.log(i);
}
What problem is caused by the code below?
The code won’t run because the stop condition is less than the start condition.
The code will loop forever because the start condition is i = 10, the stop condition is i > 0, and we are adding to i rather than subtracting. The iterator condition should be i–.
You can’t run a loop without an array.
i = 10 and i > 0 should be switched.
The code will loop forever because the start condition is i = 10, the stop condition is i > 0, and we are adding to i rather than subtracting. The iterator condition should be i–.