Loops Flashcards
What are loops?
Way of repeating action N times
What are the three main types of loops in JS?
- For
- While
- Do while
Example of for loop?
for (let i = 1; 1 < 5; i++) { console.log(i) }
What 3 elements are in a “for loop”?
InitialExpression = where loop start conditionExpression = where loop end IncrementExpression = how much it goes up or down by
What does the “i” stand for in a loop?
It stand for increment
How can you describe with words the following loops?
for (let i = 1; i < 5; i++) { consol.log(i) }
- 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
Make a function that prints “21” 21 times
function 21(){ for(let 1 = 1; 1 < 22; i++){ consol.log("21") } } 21()
How to put something into the doms (document)?
document.querySelector(“#id”).innertext
How is the syntax of a while loop?
let count = 0
while(count < 5){
console.log(count)
count++
}
What is the syntax for a Do while loop?
do { // loop body } while (condition);
How can a part of the For loop be skipped?
yes by just leaving the space for (; i < 3; i++)
What happen when we run this loop?
for (;;) {
…
}
it will repeat witout limits
What are the differences between the 3 different type of loops?
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.
What is the latest value alerted by this code? let i = 3;
while (i) {
alert( i– );
}
Every loop iteration decreases i by 1. The check while(i) stops the loop when i = 0.