Tell us what’s happening:
can somwone please explain step by step on HOW this works because im a little confused
**Your code so far**
function nextInLine(arr, item) {
arr.push(item);
return arr.shift();
}
const testArr = [1, 2, 3, 4, 5];
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15
Challenge: Stand in Line
Link to the challenge:
When you push() you add to your stack (array).
When you shift() you move your stack (array) to the left, thus, bumping your first integer.
The trick is to observe what is happening at both ends of your array (stack of numbers)
arr.push(item) affects both ends (it adds a new number and shifts the first)
const removed (remember its a constant, so you can name it how you want like i.e “kickedout”)
function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
const kickedout = arr.shift();
return kickedout;
// 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));
system
Closed
3
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.