Files
Sidak Singh Aulakh fe085246c8 Use the Spread Operator to Evaluate Arrays In-Place, adding the proper solution. (Fixes #35022) (#35037)
* Adding proper solution for spread operation

Use the Spread Operator to Evaluate Arrays In-Place: adding the proper solution for the challenge

* made some more changes

* Fixed the Solution in es6 class constructor guide

Fixed the vegetable solution in Guide: Use class Syntax to Define a Constructor Function
2019-02-05 11:39:52 +03:00

1.8 KiB

title
title
Use the Spread Operator to Evaluate Arrays In-Place

Use the Spread Operator to Evaluate Arrays In-Place

Spread Operator explained

Mozilla Developer Network Spread Operator

Spread Operator compared to Rest Parameter

Stack Overflow

Video Explaining Spread Operator and Rest Parameter

Image of youtube video link spread and rest operator

Information About apply() Method

Mozilla Developer Network Apply Method

3 Quick Examples

let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min.apply(null, numbers);
console.log(minNum);//-12
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min(numbers);
console.log(minNum);//NaN 
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min(...numbers);
console.log(minNum);//-12

SPOILER WARNING: SOLUTION AHEAD

The Solution

Unpacking the arr1 using the spread operator and then copying those values to arr2

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
  "use strict";
  arr2 = [...arr1]; // change this line
})();
console.log(arr2);