Loops Flashcards

1
Q

for - loops

A

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

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

for/in - loops

A

for/in - loops through the properties of an object

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

while - loops

A

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

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

do/while - loops

A

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

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

Review: Loops

A

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.

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

Nested For Loop syntax

A
for (let i = 0; i < myArray.length; i++) {
  for (let j = 0; j < yourArray.length; j++) {
    //Code To Run
   }
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

While Loop syntax

A
while (condition) {
  // code block that loops until condition is false
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

While Loop example

A

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

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

For Loop example

A

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] + '.');
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

while loops run an infinite amount of times as long as their condition remains true.

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

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

A

i < 10

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

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]

A

‘Grizzly Bear’
‘Sloth’
‘Sea Lion’

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

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.

A

To compare two lists.

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

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.

A

for loops always loop over a code block a known amount of times.

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

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.

A

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

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

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.

A

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