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

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?

I’m just wondering, could you use

s.sort()

to achieve the same result?

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

Thanks a bunch!

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().