Loops Flashcards Preview

JavaScript Basics > Loops > Flashcards

Flashcards in Loops Deck (16)
Loading flashcards...
1

for - loops

for - loops through a block of code a number of times


2

for/in - loops

for/in - loops through the properties of an object

3

while - loops

while - loops through a block of code while a specified condition is true

4

do/while - loops

do/while - also loops through a block of code while a specified condition is true

5

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.

6

Nested For Loop syntax

for (let i = 0; i < myArray.length; i++) {
for (let j = 0; j < yourArray.length; j++) {
//Code To Run
}
}

7

While Loop syntax

while (condition) {
// code block that loops until condition is false
}

8

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');

9

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] + '.');
}

10

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.

11

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

12

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'

13

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.

14

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.

15

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--.

16

What is incorrect about the code below?

for (let i = 0, i < myArray.length, i++) {
console.log(myArray[i]);
}
What is incorrect about the code below?

The incrementer should be i+=1.


The loop's conditions should be separated by semicolons instead of commas.


let is not needed for the start condition.


i++ should be i = i + 1.

The loop's conditions should be separated by semicolons instead of commas.