JavaScript: Iteration Flashcards

1
Q

Why is a “for loop” used?

A

We use a for loop to execute code more than once.

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

What are the three parts of a “for loop?”
What is the syntax?

A
  1. declare counter (index)
  2. declare expression saying how long the loop will run (end condition)
  3. How to increment or decrement your counter variable

for (let i = 0; i <= 1; i++) {
console.log(‘This is the current value of i: ${i}.`);
}

Note: i++ is shorthand for i + 1

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

How would you use a “for loop” to create a numbered list of all data in the array?
What is the syntax?

A

const zooAnimals = [“Bears”, “Giraffes”, “Penguins”, “Meerkats”];

for (let i = 0; i < zooAnimals.length; i++) {
console.log( ‘${i + 1}) ${zooAnimals[i]}`);
}

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

How would you use a “for loop” to create a list of all data in the array (without index numbers)?
What is the syntax?

A

const zooAnimals = [“Bears”, “Giraffes”, “Penguins”, “Meerkats”];

for (const animal of zooAnimals) {
console.log(animal);
}

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