Rest parameters and spread syntax Flashcards

1
Q

function sum(a, b) {
return a + b;
}

alert( sum(1, 2, 3, 4, 5) ); // returns?

A

3

rest ignored

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

function f(arg1, …rest, arg2) { // arg2 after …rest ?!
// returns and why
}

A

error

spread must be at end

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

let arr = [3, 5, 1];
alert( Math.max(arr) ); // NaN

how can we fix the above to return 5

A

alert( Math.max(…arr) );

spread operator

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

let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];

alert( Math.max(…arr1, …arr2) ); //

A

8

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

let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];

alert(/* write code here */ ); // 8

how to get max number

A

Math.max(…arr1, …arr2)

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

let arr1 = [1, -2, 3, 4];
let arr2 = [8, 3, -8, 1];

alert( Math.max(1, …arr1, 2, …arr2, 25) ); // returns?

A

25

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

let arr = [3, 5, 1];
let arr2 = [8, 9, 15];

let merged = [0, …arr, 2, …arr2];

alert(merged); //

A

0,3,5,1,2,8,9,15

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

let arr = [3, 5, 1];
let arr2 = [8, 9, 15];

let merged = /* write code here */

alert(merged); // 0,3,5,1,2,8,9,15

A

[0, …arr, 2, …arr2];

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

let str = “Hello”;

alert( /* write code here*/ ); // H,e,l,l,o

A

[…str]

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

let str = “Hello”;
alert( […str] ); // H,e,l,l,o

the above is the same as______

A

Array.from(str)

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

When … is at the end of function parameters, it’s “rest parameters” and _______

A

gathers the rest of the list of arguments into an array.

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