Loops Flashcards

1
Q

What are loops?

A

Way of repeating action N times

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

What are the three main types of loops in JS?

A
  • For
  • While
  • Do while
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Example of for loop?

A
for (let i = 1; 1 < 5; i++) {
console.log(i)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What 3 elements are in a “for loop”?

A
InitialExpression = where loop start
conditionExpression = where loop end
IncrementExpression = how much it goes up or down by
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the “i” stand for in a loop?

A

It stand for increment

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

How can you describe with words the following loops?

for (let i = 1; i < 5; i++) {
    consol.log(i)
}
A
  • The loop starts at let=1
  • At the end of the first cycle the the loops check if 1 is < than 5 which is true
  • So it does what is inside the curly braces
  • Then when done it does the “i++”
  • And so on until i is not longer less than 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Make a function that prints “21” 21 times

A
function 21(){
    for(let 1 = 1; 1 < 22; i++){
      consol.log("21")
    }
}
 21()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to put something into the doms (document)?

A

document.querySelector(“#id”).innertext

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

How is the syntax of a while loop?

A

let count = 0

while(count < 5){
console.log(count)
count++
}

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

What is the syntax for a Do while loop?

A
do {
    // loop body
  } while (condition);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can a part of the For loop be skipped?

A

yes by just leaving the space for (; i < 3; i++)

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

What happen when we run this loop?
for (;;) {

}

A

it will repeat witout limits

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

What are the differences between the 3 different type of loops?

A

while – The condition is checked before each iteration.
do..while – The condition is checked after each iteration.
for (;;) – The condition is checked before each iteration, additional settings available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What is the latest value alerted by this code?
let i = 3;

while (i) {
alert( i– );
}

A

Every loop iteration decreases i by 1. The check while(i) stops the loop when i = 0.

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