Exercise on Spread Operator and Rest Parameters
Get a hang of the spread operator and rest parameters by trying out these exercises. Remember, the point is to think differently and move away from ES5 conventions.
Exercise 1:
Make a shallow copy of an array of any length in one destructuring assignment! The given array contains random integers, can be of any length and is called originalArray
. Call the new array you make clonedArray
. Remember to use this exact spelling or your code won’t compile.
If you don’t know what a shallow copy is, make sure you read about it, as you will need these concepts during your programming career. I can highly recommend my article on Cloning Objects in JavaScript.
// Original array randomly generatedconsole.log(originalArray);let clonedArray = []
Exercise 2:
Determine the value logged to the console without running it.
let f = () => [..."12345"];let A = f().map( f );console.log( A );
Explanation
An array of five vectors of ['1', '2', '3', '4', '5']
is printed out as a table. The mechanism is the exact same as the explanation in the ...