At 'Stand in line' lesson in Basic JavaScript, testArr is a const

Tell us what’s happening:

At https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/stand-in-line, the testArr variable is declared as a constant ― even if I press ‘Reset All Code’ followed ‘Reset This Lesson’, it is so.

This wouldn’t be really a problem if it weren’t for the first two comments that also come by default: “Only change code below/above this line”.

Should this be like it is?


function nextInLine(arr, item) {
// Only change code below this line
arr = arr.concat(item);
item = arr.shift()
// testArr = arr;
return item;
// Only change code above this line
}

// Setup
const testArr = [1, 2, 3, 4, 5];

// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:96.0) Gecko/20100101 Firefox/96.0

Challenge: Stand in Line

Link to the challenge:

Why do you think that the array being const is a problem?

The Lesson can be completed by declaring testArr with var or let at the Setup btw. It’s just the comments that made me confused.

You don’t need to make any changes to testArr. You should not touch any global variables. If you can pass this challenge by modifying a global variable, that is a bug in the test suite.

I wasn’t really sure about the post, sorry. What led me to make it was that running it as it is (the first one) always results in After nextInLine(testArr, 10), testArr[4] should be 10, where just changing testArr to let or var and uncommenting line 5 completed the lesson.

If that’s the case, that is a bug. Zero changes below that line are needed.

Even then, at // Display code, since testArr is a const and can’t be altered, what is the use of of console.log("After: " + JSON.stringify(testArr));?

const doesn’t mean you can’t change the contents of the array, only which array your variable points at.

1 Like

Oh, I think I understand now then. My solution didn’t work because concat fuses two arrays, and creates a new one? And that doesn’t work since const?

The solution provided at freeCodeCamp Challenge Guide: Stand in Line worked just fine without alterations to the Setup, and the // Display code shows the changes properly.

My mistake, sorry and thanks for the help.

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