Use a recursive function to update an array declared in a const

Hi everyone,

I was wondering if there was a way to make the const change with an automated function (recursive) ?
I would want to find a recursive way to make it happen.

I thought of the following : (sorry for the const names, i can correct it if possible).

function fillArray() {
const d = [1, 2, 3];
d.fill(1, 2);
console.log(d);
return d;
}
fillArray();

I just discovered this property (fill), which is not going well.

  **Your code so far**
const s = [5, 7, 2];
function editInPlace() {
// Only change code below this line
s[0] = 2;
s[1] = 5;
s[2] = 7;
// 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/105.0.0.0 Safari/537.36

Challenge: ES6 - Mutate an Array Declared with const

Link to the challenge:

I don’t understand what you are asking.

A const declaration for a variable holding an array means that you will use the same array thoughout the life of that variable, but you are free to change the contents of that array however you like.

1 Like

This is insane on how you answered this fast, wow.

To be more precise :
I would want to know if there’s a way to modify an array based on a const using a recursive function.

Sorry if my question is not making sense at all, i am still struggling to formulate my technical knowledge on this field.

Your explaination was really good ! Thanks, i think i am over thinking and my answer will naturally come as the time pass.

You can modify the contents of that array however you like. You just can’t change which array is stored in that variable.

const myArray = [1, 2, 3];

function addEntries(arr, n) {
  if (n <= 0) {
    return 0;
  } else {
    return arr.push(n) + addEntries(arr, n - 1);
  }
}

addEntries(myArray, 5);
console.log(myArray);

this should work fine

1 Like

This was exactly what i was looking for !
Thank you a lot for your help :slight_smile:

I will save it and check it every day to memorize the path to take.

You rock !!