Loops Flashcards
Do a block. Then Continue doing it while a condition is true
do {
}
while ( )
If a condition is true keep doing a block with local scope conditional and variables
for (let i = 0; i < 4; i ++) {
}
If a condition is true, do a block. Continue if still true
while ( ) {
}
Do a function for each array element (method)
myArray.forEach(element => )
Return a new array based off another’s elements and a callback function (arrays, not array like objects).
What’s the one for array-like objects?
myArray.map(element => )
Array.from() is the one for array-like objects
End a loop
Stop and loop and return something
Skip to the next loop iteration
break
return
continue
Return the type of data
(will return a string)
typeof input
Return boolean whether argument is an array or not
Array.isArray( )
Do a block with every element in an array or character in string.
for (let (…element) of …)
for (let variableName of iterableArrayOrFunction) {
statement
}
Do a block with each key in an array, object, or function
for (let (…key) in …
What elements of:
for (let i = 0; i < 4; i ==) {
}
Can be skipped
all of them, you can do:
for (;;) {
}
it will just loop endlessly
Expected output // 1, then 3, 5, 7, 9
Both of these will give the output:
for (let i = 0; i < 10; i++) { if (i % 2 == 0) continue; alert(i); }
vs
for (let i = 0; i < 10; i++) { if (i % 2) { alert( i ); }
}
But which should you use and why?
The first one is nicer because it reduces nesting
nesting bad because it can be hard to read??
What does break do here?
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let input = prompt(`Value at coords (${i},${j})`, ''); break; } }
only breaks from first loop
Break from multiple nested loops.
Use labels: outerLabel: for (let i = 0; i < 3; i++) { //(*)
for (let j = 0; j < 3; j++) {
let input = prompt(`Value at coords (${i},${j})`, '');
// if an empty string or canceled, then break out of both loops if (!input) break outerLabel; // (*)
// do something with the value... } } alert('Done!');
Can you use arbitary expressions in switch statements? For example: switch () { case b + 1 : console.log('etc'); break; }
Yes
Do switch statements use equality or strict equality?
Strict
What is returned from:
(i > 5) ? alert(i) : continue;
It throws an error its not a loop so continue doesn’t do anything
What’s the difference between:
for (let… in …)
for (let… of …)
for (let …)
for (let… in …) - The for…in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
for (let variableAsString in object) {
statement
}
for (let... of ...) - a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. for (variable of iterable) { statement } for (let (...element) of ...) for (let variableName of iterableArrayOrFunction) { statement }
for (let …) - The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.
for ([initialization]; [condition]; [final-expression])
statement
can do:
for (;;) {
}