A challenge of "Spread Operator" works well even if I don't put the Spread Operator

A challenge of “Spread Operator” works well even if I don’t put the Spread Operator:
CHALLENGE: " ES6: Use the Spread Operator to Evaluate Arrays In-Place"
I have commented/deleted the right code and put another code without Spread Operator >> The challenge passed successfully!

Tip 2 didn’t implemented as well: ... spread operator should be used to duplicate arr1 .

The Code mentioned below


const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

// arr2 = [...arr1];  // Change this line
arr2 = arr1;
console.log(arr2);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Use the Spread Operator to Evaluate Arrays In-Place

Link to the challenge:

It looks like this only works because you have the correct answer commented out.

It works even if I deleted the commented code.

Can you show me code that passes with that line deleted? If I copy and paste your code and delete the line

// arr2 = [...arr1];  // Change this line

then it fails.

1 Like

Maybe It worked well because I run it twice,
first time with the commented code and the second when it was deleted…

This is because they both do the same thing, but you were required to use the spread operator for this particular exercise.

arr2 = arr1 (copies arr1 to arr2)
arr2 = […arr1] (spread operator)

The do not do the same thing.

arr2 = arr1 makes a shallow copy. If you make changes in arr2, then arr1 is changed.

arr2 = [...arr1] makes a deep copy. If you make changes in arr2, then arr1 is unchanged.

It’s a small piece of syntax that can cause huge headaches in debugging.

2 Likes

I have moved this to the #contributors sub-forum.

Whilst the mentioned solution is incorrect, it does not go against the aim of the lesson - teaching students to use the spread operator. Yes, you have not done as expected, but you had to write the code necessary to make a shallow copy (albeit in comment). So, clearly, you understood the use of the operator.

All in all, I do not see a need to change the tests.