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 (;;) {
}
What is logged to console from:
let myObject = { 1: 1, 2: 2, 3: 3, }
for (value of myObject) {
console.log(value);
}
Error,
myObject is not iterable
What must a value have for it to be iterable (by the for of loop for instance)?
It must have the ‘next’ method
a Symbol.iterator key that returns an object with the next() method that returns an object { done: true or false, value: the value }
What is the algorithm for the for of loop? (4 steps)
- When for..of starts, it calls that method:
range[Symbol.iterator] = function() {
return Iterator object…
once (or errors if not found). The method must return an iterator – an object with the method next. - Onward, for..of works only with that returned object.
- When for..of wants the next value, it calls next() on that object.
- The result of next() must have the form {done: Boolean, value: any}, where done=true means that the loop is finished, otherwise value is the next value.
Create an iterable object that will receive the values 1 through 5 from this object: let range = { from: 1, to: 5 };
let range = {
from: 1,
to: 5
};
// 1. call to for..of initially calls this range[Symbol.iterator] = function() { // ...it returns the iterator object:
// 2. Onward, for..of works only with the iterator object below, asking it for next values
return {
current: this.from,
last: this.to,
// 3. next() is called on each iteration by the for..of loop next() {
// 4. it should return the value as an object {done:.., value :...} if (this.current <= this.last) { return { done: false, value: this.current++ }; } else { return { done: true }; } } }; };
// now it works! for (let num of range) { alert(num); // 1, then 2, 3, 4, 5 }
The following is incorrect.
T or F
let range = {
from: 1,
to: 5
};
range[Symbol.iterator] = function() {
return {
current: this.from,
last: this.to,
next() { if (this.current <= this.last) { return { done: false, value: this.current++ }; } else { return { done: true }; } } }; };
False
An iterator is created by the call to the Symbol.iterator method
For the iteration to continue the symbol.iterator method has to return an object with the next() method on it.
Adding Symbol.iterator inside an object does not make the object properties iterable. But instead calls upon another object to set the rules of iteration
T or F
True
BUT The iterator looks for the next() method in whatever object is returned from myObject.Symbol.iterator.
So you can return the outer object in range.Symbol.iterator (return this) and have the next() method there.
let range = {
from: 1,
to: 5,
Symbol.iterator {
this.current = this.from;
return this;
},
next() { if (this.current <= this.to) { return { done: false, value: this.current++ }; } else { return { done: true }; } } };
for (let num of range) {
alert(num); // 1, then 2, 3, 4, 5
}
The downside is that now it’s impossible to have two for..of loops running over the object simultaneously: they’ll share the iteration state, because there’s only one iterator – the object itself. But two parallel for-ofs is a rare thing, even in async scenarios.