Mutate an Array Declared with const property question

Mutate an Array Declared with const property question
0

#1

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


#2

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


#3

Damn, didn’t notice. Thank you


#4

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


#5

How could you use that to achieve the same result?


#6

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


#7

I’m just wondering, could you use

s.sort()

to achieve the same result?


#8

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.


#9

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

Thanks a bunch!


#10

But using sort defeats the whole purpose of the lesson.


#11

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