.ES6: Mutate an Array Declared with const

why does it say

// running tests
should be equal to
[2, 5, 7]
. // tests completed
const s = [5, 7, 2];
function editInPlace() {
// Only change code below this line
const s = [2, 5, 7];
function editInPlace() {
"use strict";
s[0] = 2;
s[1] = 5;
s[2] = 7;
}
editInPlace();
// Using s = [2, 5, 7] would be invalid
//how the ****
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Mutate an Array Declared with const

Link to the challenge:

1 Like

Your code seems a little scrambled. When I unscramble it, it passes for me.

could u tell me how plz

first your function signature is there twice, make sure you have only once

1 Like

thanks but still not done

Why are you re-declaring s? This re-declaration is shadowing the other variable s and your code does not change the outer variable s.

1 Like

sry what
more detail

what’s your code now?

It looks like you just copy-pasted the solution into the starter code without understanding it.

I’d step back and reset your code.

const s = [5, 7, 2];
function editInPlace() {
  // Only change code below this line
const s = [2, 5, 7]; {
  "use strict";
  s[0] = 2;
  s[1] = 5;
  s[2] = 7;
}
editInPlace();
  // Using s = [2, 5, 7] would be invalid
  // Only change code above this line
}

yup i have no idea about this one

calling a function from inside itself is also a bad idea, remove that function call or you get a stackoverflow error

3 Likes

Ok. I would reset your code. Copy-pasting stuff without understanding will not work.

The starter code is this:

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

  // Using s = [2, 5, 7] would be invalid

  // Only change code above this line
}
editInPlace();

You need to put something inside the function that changes s from [5, 7, 2] to [2, 5, 7]. But

s = [2, 5, 7];

would throw an error because s is const.

So you need to update the value at each index in the array one at a time.

2 Likes

Right, your approach of

s[0] = 2;
s[1] = 5;
s[2] = 7;

is good, but you’ve messed up your code with other stuff. Reset the code to get a fresh start and then put those 3 lines in the right place. If I do that, it passes for me.

1 Like

thanks so much that worked

also how do u remove the ES5 assignment syntax.

The whole point of the exercise is that you must set the individual entries of the array and cannot replace the array because the variable was declared with const.

1 Like

Yeah, maybe Jeremy cleared it up, but I’m not sure what you mean by “ES5 assignment syntax”.

1 Like

neither do i know but dosn’t matter

OK, then, don’t worry about it. Good job on this challenge, have fun on the next one.

1 Like