Loops Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

what’s the difference between for and for ... of?

A

for (initialiser; condition; final expression )
or can express as
for (begin ; condition ; step )
likely to be error prone as there are more conditions to set

for (const item of array)
iterates through each item in an array or iterable object and applies code

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

what’s the syntax for a for loop - specifying a piece of code to run a certain number of times? explain each part. give a concrete example

A

for (initialiser; condition; final expression )

INITIALISER/BEGIN: counts number of times loop has run
can also be declared right in the loop

CONDITION: specifies when the condition turns falsy and the loop should stop running

FINAL EXPRESSION/STEP: instruction to increment/decrement counter variable after each loop to bring it closer to a false condition state

CONCRETE EXAMPLE:
for (let i = 1; i < 10; i++) {…};

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

when might the standard for loop be useful over for ... of?

A

When we need to iterate over object that supports it and we need to access index position of each item

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

how can we exit loops?

A

break;

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

how can we skip iterations?

A

continue;

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

what is the difference between while and do...while (syntax, function)?

A

let i = num
while (cond) {
final expression
}

generally preferred over do while

 let i = num
 do {
      final expression
 } while (cond);

only use if you want the code to execute at least once no matter the condition as the cond is evaluated after the code runs

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

what are 2 function expressions that can be used to also loop through code?

A

let newArray = collection.map(function)
returns a new away with the changed items after applying a specific code to them

let newArray = collection.filter(function);
returns a new array with just the items that match a given cond

where collection is the array storing multiple items

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

which loop is recommended for beginners? and why?

A

for loop
easier to keep track of settings as they’re contained in one place

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

how do we break out of multiple nested loops at the same time?

A

using labels - we apply a label to the loop we want to break to (e.g. outer) then call it again later on break outer

outer: for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
//code

// if an empty string or canceled, then break out of both loops
if (!input) break outer; // (*)

// do something with the value...   } }

alert(‘Done!’);

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

what happens if we omit any part of the for loop?

A

if INITIALISER is omitted
for ( ; i < 5 ; i++);
can do this if we’ve already specified a value to the counter variable first e.g. let i = 0

if CONDITION is omitted

if FINAL EXPRESSION is omitted
for ( ; i < 5 ; i++);
makes the loop identical towhile (i < 3)

if everything is omitted
for ( ;; )
infinite loop

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

how can we make an infinite loop?

A

while (true)

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