Stand in line js challenge - forced tests but how can I improve?

Stand in line js challenge - forced tests but how can I improve?
0.0 0

#1

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));

#2
function nextLine(arr,item) {
    var arr = arr || [];
    if(typeof item === 'undefined') {
        return;
    }
    arr.push(item); // store item at the end of array
    return arr.shift();
}

#3

ohhh thank you, can’t believe I didn’t think to use return before shift, thanks!


#4

No problem it was my pleasure to help. Javascript is not an easy peasy piece so we all struggle :slight_smile: 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.