Mutate an Array Declared with const property question

Tell us what’s happening:

Hello, I tried to assign values like in the example, but am getting an error.

Your code so far


const s = [5, 7, 2];
function editInPlace() {
  "use strict";
  // change code below this line

  // s = [2, 5, 7]; <- this is invalid
s[0] =2;
s[1]=5;
2[2]=7;
  // change code above this line

}
editInPlace();

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const

You appear to have a typo on the line above. 2 is not an array.

Damn, didn’t notice. Thank you

s.sort()
that’s it instead of all of this.

2 Likes

How could you use that to achieve the same result?

When you refer to “that”, what are you talking about specifically?

I’m just wondering, could you use

s.sort()

to achieve the same result?

Yes, in this specific example of s being [5, 7, 2]. If the instructions would have said change [5, 7, 10, 3] to [10, 3, 7, 5], s.sort() would be useless.

Oh, that’s great to know and look into,

Thanks a bunch!

But using sort defeats the whole purpose of the lesson.

You can also use

var x = s.pop();
s.unshift(x);

to bring back some earlier lesson info. it is a little more useful than sort().