Javascript (Loops) Flashcards

1
Q

What are loops?

A

Loops allow us to tell computers to repeat a given block of code on its own so we do not have to write out the same code over and over.

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

The for loop

A

The for loop is one of the most classic loops in programming. We start by defining the 3 properties of the loop with semicolon, and then we add a block that’s executed for every iteration:

for (<initialization>; <condition>; <increment>) {</increment></condition></initialization>

}

This <initialization>; <condition>; <increment> setup is very powerful, because we can do things like looping in the opposite direction, or skip items, or only iterate a portion of an array, but it’s also quite tricky to learn at first.</increment></condition></initialization>

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

What are the three expressions in a for loop?

A
  1. initialization (initializer) - The initialization defines where to begin the loop by declaring (or referencing) the iterator variable

2.a stopping condition- The stopping condition determines when to stop looping (when the expression evaluates to false)

  1. The iteration statement updates the iterator each time the loop is completed

Ex:
for (let counter = 0; counter < 4; counter++) {
console.log(counter);
}

// The following is printed:
0
1
2
3

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

What is a do while loop/statement?

A

The do..while loop might be less powerful than for loop but it’s also simpler to setup. With this loop we do something, which we define in a block, and then we decide if we want to do it again.

We have to define an index variable outside of it before we can use it, so it’s more verbose than for.

const list = [‘a’, ‘b’, ‘c’]

let i = 0

do {
console.log(list[i])
i = i + 1
} while (i < list.length)

Notice that we also have to increment i manually. If we forget to do so, we will create what’s called an infinite loop.

You can interrupt a while loop using break:

do {
if (something) break
} while (true)

and you can jump to the next iteration using continue:

do {
if (something) continue

//do something else
} while (true)

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

What are two additional actions we can do with a do while loop?

A
  1. We can interrupt a while loop using break:

do {
if (something) break
} while (true)

  1. We can jump to the next iteration using continue:

do {
if (something) continue

//do something else
} while (true)

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

The while loop

A

The while loop is similar to do..while, but there’s a key difference: with while we first check the condition and then (maybe) we do something.

With do..while, if you remember, first we did something and then we checked if we wanted to do it again.

So it’s a similar loop, but for a different use case.

Same as do..while we have to define and increment i manually to avoid an infinite loop.

const list = [‘a’, ‘b’, ‘c’]

let i = 0

while (i < list.length) {
console.log(list[i])
i = i + 1
}

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

What are two additional actions we can do with a while loop?

A
  1. We can interrupt a while loop using break:

while (true) {
if (something) break
}

  1. We can jump to the next iteration using continue:

while (true) {
if (something) continue

//do something else
}

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

When should you use a while loop?

A

Its best to use the while loop when we don’t know in advance how many times the loop should run.
Think of it like eating. When you are eating you don’t know how many bites you are going to need to become full. You continue to eat while you are hungry.

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

What is the break keyword?

A

The break keyword allows programs to “break” out of the loop from within the loop’s block.

Ex:
for (let i = 0; i < 99; i++) {
if (i > 2 ) {
break;
}
console.log(‘Banana.’);
}

console.log(‘Orange you glad I broke out the loop!’);

//Print
Banana.
Banana.
Banana.
Orange you glad I broke out the loop!

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

How do we run a backward for loop?

A
  1. Set the iterator variable to the highest desired value in the initialization expression.
  2. Set the stopping condition for when the iterator variable is less than the desired amount.
  3. The iterator should decrease in intervals after each iteration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are nested loops?

A

When we have a loop running inside another loop, we call that a nested loop.

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

What is one way we can use a nested for loop?

A

To compare the elements in two arrays.

Ex:

const myArray = [6, 19, 20];
const yourArray = [19, 81, 2];
for (let i = 0; i < myArray.length; i++) {
for (let j = 0; j < yourArray.length; j++) {
if (myArray[i] === yourArray[j]) {
console.log(‘Both arrays have the number: ‘ + yourArray[j]);
}
}
}

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

What are infinite loops?

A

a loop that executes a statement or a block of statements repeatedly, without a guarding condition to determine its end

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

The for-of loop

A

The for…of loop is a very simple kind of loop.

With this loop you don’t have to worry about the index.

const list = [‘a’, ‘b’, ‘c’]

for (const item of list) {
console.log(item)
}
It’s much simpler but one downside is that we don’t have a way to get the index value, which is often useful.

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

What can we do to combat the downside of the for-of loop (where we don’t have a way to get the index value)

A

In this case we use a “trick” by combining the array destructuring syntax and calling the entries() method on the array, which we’ll talk more about later on when we’ll get to iterators:

for (const [index, value] of [‘a’, ‘b’, ‘c’].entries()) {
console.log(index)
console.log(value)
}
Notice the use of const when we define item. This loop creates a new scope in every iteration, so we can safely use that instead of let. This is not something we can do for the for loop, where we have to use let.

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

JS for…in loop

A

The JavaScript for…in loop can be used to iterate over the keys of an object. In each iteration, one of the properties from the object is assigned to the variable of that loop.

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
console.log(${property}: ${object[property]});
}

// Expected output:
// “a: 1”
// “b: 2”
// “c: 3”

const dog = { name: ‘Roger’, color: ‘gray’ }

for (let property in dog) {
console.log(property) // ‘name’ in the first iteration
// ‘color’ in the second

console.log(dog[property]) // ‘Roger’ in the first iteration
// ‘gray’ in the second
}
Since arrays are a special kind of object, we can iterate over the items in an array too:

const dogs = [‘Roger’, ‘Vanille’]

for (let index in dogs) {
console.log(dogs[index])
}

17
Q

Why do we have to use a for … in loop to iterate over the keys of an object?

A

Because key-value pairs in objects are not ordered, which means we cannot iterate through them using numerical indexing like we can do with arrays.

18
Q

What are two additional loop types in JS that are methods?

A
  1. Array.forEach()
  2. Array.map()