Mutating JS Arrays Declare with const

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

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

// Only 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/90.0.4430.93 Safari/537.36.

Challenge: Mutate an Array Declared with const

Link to the challenge:

Please ask questions so we don’t have to dig around to figure out what the problem is.

But looking around your code, remember that JS variables are case sensitive.

1 Like

The answer appears to be in the description

s[2] = 45;

Just replacing the numbers in a similar way, e.g., s[0] = 2; , etc., should pass this.

2 Likes

You declare an array:

const S = [2, 5, 7];

This means that the first item (index 0) is 2, the second item is 5 and the third is 7.

You can log the items:

console.log(S[0]) // 2
console.log(S[1]) // 5
console.log(S[2]) // 7

Setting them again to those values changes absolutely nothing, this is pointless code:

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

because S[0], S[1] and S[2] already have those values.

This was the original code 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();

Ok, also another moderator changed the name of my heading to be more specific so I just copied and pasted the same thing.

It didn’t work, I got the same error telling me that s should be “equal to 2, 5,7”.

Hi!

If you’re still getting an error with this code… There’s a small typo - the array s declared in the beginning is in lowercase…

As JS is case-sensitive, you’re not changing the target array here…

You have misunderstood what the challenge wants you to do. You’re not supposed to create a new array S (uppercase), but to change the existing array s (lowercase).

Because it was declared with const, you can’t just reassign it to a new array. Instead you have to change each item by accessing it directly through its index: s[0] , s[1] etc.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.