Misunderstanding with "Mutate an Array Declared with const"

Hello,

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/mutate-an-array-declared-with-const
If I may, since “s” is used in the function, thus “s” should also be placed as a parameter of the function, this for a better understanding of the exercise. Since this function only works with the array “s”.

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

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

Adding s as an argument fundamentally changes how this function works. Without the argument, this function uses the global variable s. With the argument, the function works for any array passed into the function. Very different behavior.

If you want use an argument, you must put a parameter. Is it correct ?


Furthermore the name of the array always will be “s”.
So I don’t understand why there isn’t “s” nowhere.

If you want to use an argument, then yes you have to add an argument. But we aren’t using an argument here. We’re directly referencing the global variable.

It will pass if you add both the parameter and the argument.

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

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

But as said, you should try not to make changes you are not asked to make. Stick to the requirements as outlined in the challenges.

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