Hello community, I’ve spent hours trying so many variations and I am stuck. I am hoping for some suggestions on what’s wrong with my code (not necessarily the solution as I need only a few pointers )
There are four required criteria to complete the exercise; yet my code passes off as completing #1,#2, & #4 of the items required (even though I have only added code that should address two of the four requirements)
#1 nextInLine([], 1) should return 1
#2 nextInLine([2], 1) should return 2
#3 nextInLine([5,6,7,8,9], 1) should return 5
#4 After nextInLine(testArr, 10), testArr[4] should be 10
Code output:
Before:[1,2,3,4,5]
1
2
After:[3,4,5,1,10]
Here is the code:
function nextInLine(arr, item) {
testArr.push(item); //pushes 1 onto end of array and returns value of that location
item=testArr.shift(item); //removes 1st element --> yet "10" is pushed to the end
return item;
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 1)); // Modify this line to test
console.log(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
…
Here is the solution that member fuqted helped me resolve the issue I was having:
function nextInLine(arr, item) {
arr.push(item); //pushes 1 onto end of array and returns value of that location
// arr.shift();
item=arr.shift(); //removes 1st element --> yet “10” is pushed to the end
return item;
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 1)); // Modify this line to test
console.log(nextInLine(testArr, 10)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));