Stand in Line | Free Code Camp

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

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  item = arr.shift();
  return item;  // Change this line
}

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 10), testArr[4]); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,562评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,359评论 0 33
  • 今天是高考第一天。长这么大,我似乎还是没能摆脱考试的笼罩。从小学开始,一直到现在,考试似乎都是我人生中很重要的一块...
    Forri阅读 1,540评论 0 0
  • 显示当前规则 iptables -L -n --line-numbers 清除内置规则 iptables -F 清...
    bailongxian阅读 1,823评论 0 0
  • 信誓旦旦的爱情,从年轻的口中说出,我们总是理所当然,以为所有的爱都会开花结果。仿佛生命的旅途,有一人注定与我们相遇...
    钟大晓阅读 4,476评论 0 1

友情链接更多精彩内容