Wpis z mikrobloga

Mam problem ze zrobieniem takiego zadania na FCC:

In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

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.

nextInLine([], 1) should return 1
nextInLine([2], 1) should return 2
nextInLine([5,6,7,8,9], 1) should return 5
After nextInLine(testArr, 10), testArr[4] should be 10

http://codepen.io/frontendowiec/pen/YGQEXp?editors=1111

Mógłby mi ktoś pomóc z tym zadaniem?

#nieprogramowanie
  • 6
@kyaroru: dla dwóch ostatnich wywołań funkcji mam zwracane złe liczby. Może po prostu nie zrozumiałem dokładnie o co w tym chodzi po angielsku.
Kopiuję wartość z pierwszego indeksu i dodaję jako ostatni element. Usuwam ten pierwszy. I tyle. Czy tutaj źle coś zrozumiałem?
@kyaroru:
function nextInLine(arr, item) {
testArr.push(testArr[0]); //pierwszy element idzie na koniec
testArr.shift(); //usuwam pierwszy element
testArr.push(item); //dodaje argument item na koniec kolejki
return item; // Change this line //może tutaj mam coś innego zwrócić?
}

Jeśli to tak ma być to nie zwraca tego co trzeba :(
@kyaroru: ok, zaskoło, ale nie do końca.
Dla nextInLine([5,6,7,8,9], 1) should return 5
przed wywołaniem: 3,4,5,1,1
funkcja zwraca 3
po wywołaniu jest 4,5,1,1,1
Tak wygląda funkcja:
function nextInLine(arr, item) {
var pom = testArr[0];
testArr.push(item);
testArr.shift();
return pom;
}

Może tak się nie przekazuje elementów w tablicy?
console.log(nextInLine([5,6,7,8,9], 1));