In computer system Science a queue is an summary Data framework where item are maintained in order. Brand-new items can be included at the back of the queue and old items space taken turn off from the front of the queue.

You are watching: Stand in line free code camp

Write a role nextInLine i m sorry takes selection (arr) and also a number (item) as arguments.Add the number to the finish of the array, then remove the an initial element the the array.The nextInLine function should climate return the facet that to be removed.

Here is the solution:

function nextInLine(arr, item) arr.push(item); var removed = arr.shift(); return removed;

This is most likely very an easy but i don"t understand the "is stored in removed" on fourth line that the following instance run.

Example Run:

Test nextInLine(<2,1>); runs. The nextInLine function is called. Arr becomes <2>. Item becomes 1.arr.push(item); Pushes 1 to <2>. For this reason arr is currently <2,1>.var gotten rid of = arr.shift(); removes the an initial element. For this reason arr is currently <1>. 2 has been removed and is save on computer in removed.return removed; 2 is returned.

From mine perspective, if 2 had been removed, what"s left is 1 and 1 should be keep in the variance due to the fact that when we eliminate something we except and consider only the remainder which is 1.

However I plainly understand that the removed number is 2, so the feels like a action is missing or is wrong. Is my reasonable strange?

asked might 24 "20 in ~ 16:55

1122 bronze title
add a comment |

2 answers 2

energetic earliest Votes
Example Run:

Test nextInLine(<2,1>); runs.

You space passing only arr to your function. Article is undefined

The nextInLine duty is called. Arr i do not care <2>. Item i do not care 1.

No, it doesn"t. Arr is still <2, 1>, article is still undefined.

arr.push(item); Pushes 1 to <2>. Therefore arr is now <2,1>.

Again, no. Arr.push(item) pushes unknown to the array, so that is currently <2,1,undefined>

var removed = arr.shift(); clears the first element. For this reason arr is currently <1>. 2 has actually been removed and also is save on computer in removed.

Except native the array is now <1, undefined>, that"s correct.

return removed; 2 is returned.

Correct. Change removes and also returns the very first element of one array.

answered may 24 "20 in ~ 17:06

65k1414 yellow badges117117 silver- badges176176 bronze badges
include a comment |
Array.shift() removes the an initial item in one array, and returns the item.

For example, you have actually an array: const yourArr = <"a", "b", "c", "d">

yourArr.shift() clears "a" and also returns it. Now yourArr = <"b","c","d">

const yourArr = <"a","b","c","d">console.log(yourArr.shift())console.log(yourArr)
answered might 24 "20 at 17:17


10.6k66 gold badges2626 silver- badges4848 bronze badges
add a comment |

your Answer

Thanks for contributing an answer to ridge Overflow!

Please be certain to answer the question. Provide details and also share her research!

But avoid

Asking for help, clarification, or responding to various other answers.Making statements based on opinion; ago them increase with recommendations or an individual experience.

To learn more, see our advice on writing an excellent answers.

See more: Breath Of The Dying ( Diablo 2 Breath Of The Dying, Breath Of The Dying Runeword

Draft saved
Draft discarded

Sign increase or log in in

authorize up making use of Google
authorize up making use of Facebook
authorize up making use of Email and also Password

Post together a guest

email Required, but never shown

Post together a guest


Required, however never shown

post Your price Discard

By clicking “Post your Answer”, you agree to our regards to service, privacy policy and also cookie policy

Not the answer you're feather for? Browse other questions tagged javascript or ask your own question.

The Overflow Blog
Featured ~ above Meta
how do ns pass command line disagreements to a Node.js program?
sending command line disagreements to npm manuscript
turning off eslint preeminence for a specific line
RangeError: Maximum call stack size exceeded in Javascript
array like static possible in angular js?
composing a attributes that removes variety and adds come the perform
Coding a Javascript Queue
items can be added at the back of the queue and old items room taken off from the former of the queue
Javascript an easy Algorithm - QUEUE
hot Network concerns more hot questions

inquiry feed
i ordered it to RSS
question feed To i ordered it to this RSS feed, copy and paste this URL right into your RSS reader.


ridge Overflow
stack Exchange Network
site design / logo © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.24.40826

Stack Overflow works ideal with JavaScript permitted

your privacy

By clicking “Accept all cookies”, you agree ridge Exchange can store cookie on your an equipment and disclose details in accordance through our Cookie Policy.