Ad. Javascript.info part 3 Flashcards
Advanced working with functions
two ways of thinking in javascript
Iterative thinking: the for loop:
Recursive thinking: simplify the task and call self:
Generally what is shorter solution, recursive solution or iterative solution?
A recursive solution
___________ is an internal data structure that contains details about the execution of a function
The execution context
__________ algorithm is more memory-saving:
A loop-based iteration Iterative
_________ method returns an object with two properties done and value. You can also provide a parameter to the next method to send a value to the generator.
The next()
function sum(a, b) { return a + b; }
alert( sum(1, 2, 3, 4, 5) );
RETRUNS
3
The “rest parameters” can be mentioned in a function definition with ______
three dots
function sumAll(...args) { let sum = 0;
for (let arg of args) sum += arg;
return sum;
}
alert( sumAll(1, 2, 3) ); //
6
function sumAll(\_\_\_\_\_\_\_\_\_\_\_\_) { <=== use spread operator let sum = 0;
for (let arg of args) sum += arg;
return sum;
}
alert( sumAll(1, 2, 3) ); //
…ARG
The …rest must always be ________
LAST
Arrow functions do not have an array-like object named _________ as well as _________
arguments method
this
function f(arg1, ...rest, arg2) { }
// RETURNS?
// error
…rest needs to be at the end
arrow functions do not have “arguments” because
it takes them from the outer “normal” function.
let arr = [3, 5, 1];
alert( Math.max(arr) ); //
NAN
max cant manually list items in the code
let arr = [3, 5, 1];
alert( Math.max(…arr) //
5
When using SPREAD OR REST, you are expanding a single variable into more
spread
When using SPREAD OR REST, arguments, you are collapsing all remaining arguments of a function into one array:
REST
let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
alert( Math.max(…arr1, …arr2) ); //
8
let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];
alert( Math.max(1, …arr1, 2, …arr2, 25) ); //
25
let str = “Hello”;
alert( […str] );
// THIS RETURNS
H,e,l,l,o
let str = “Hello”;
alert( …str ); //
H
The SPREAD OR ARRAY.FROM operates only on iterables.
SPREAD
SPREAD OR ARRAY.FROM operates on both array-likes and iterables.
ARRAY.FROM
an example of a recursive solution is the ___ statement
If
ARRAY.FROM
operates on both array-likes and iterables.
SPREAD
operates only on iterables.