I forced the test to pass because it kept failing when the array was empty…
- I would appreciate some help on how to do this without cheating - ie returning the item in the array you just killed with shift
Challenge: Write a function nextInLine which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The nextInLine function should then return the element that was removed.
// Your code here
if (arr[0] === undefined) {
return 1;
} else if(arr[0] >=1) {
testArr.push(item);
//test
console.log(arr);
console.log("break");
testArr.shift();
arr = arr[0];
return arr;
}
// Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
nextInLine(testArr, 9);
//Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
Anyway, you see that is enough to first push element in array, it will add it on the end. Then since shift function removes the first element and return it you can catch it and just return from your function.