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?


javascript
re-publishing
follow
asked might 24 "20 in ~ 16:55
*

CSucheCSuche
1122 bronze title
4
add a comment |

2 answers 2


energetic earliest Votes
0
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.


re-publishing
monitor
answered may 24 "20 in ~ 17:06
*

baaobaao
65k1414 yellow badges117117 silver- badges176176 bronze badges
1
include a comment |
0
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)
re-publishing
follow
answered might 24 "20 at 17:17

*

symlinksymlink
10.6k66 gold badges2626 silver- badges4848 bronze badges
1
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
submit

Post together a guest


name
email Required, but never shown


Post together a guest


name
email

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
connected
2702
how do ns pass command line disagreements to a Node.js program?
1081
sending command line disagreements to npm manuscript
1627
turning off eslint preeminence for a specific line
0
RangeError: Maximum call stack size exceeded in Javascript
0
array like static possible in angular js?
3
composing a attributes that removes variety and adds come the perform
0
Coding a Javascript Queue
0
items can be added at the back of the queue and old items room taken off from the former of the queue
1
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.


*

lang-js
ridge Overflow
commodities
firm
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.