admin管理员组

文章数量:1290095

I am working a question on FreeCodeCamp. I need to add a number to the end of the array, then remove the first element of the array. The function called nextInLine should return the element that was removed from it.

I'm not pletely new, but I'm not advanced in JS either. However, I'm new to algorithms. I've watched Youtube videos for assistance with Queues. I have this concept fairly understood as follows:

//array as queue (FIFO)
const arr = [];
arr.push(5); // [5]
arr.push(7); // [5,7]
arr.push(9); // [5,7,9]
console.log(arr.shift()); // [7,9]
console.log(arr.shift()); // [9]
console.log(arr.shift()); // []

However, when trying to implement this idea into my code, I seem to not maybe fully understand the presented problem in order to make it run. I am to modify the function nextInLine, and the ment that says //Display Code, I am to change that line as well.

function nextInLine(arr, item) {
  // Your code here
  arr.push(arr[0]);
  arr.shift();
  return console.log(arr, 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)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

When running the code, this is what is showing for me.

nextInLine([], 5) should return a number.
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

My function is outputting /* 2,3,4,5,1 */ but it seems to be incorrect. Perhaps I am failing to put it into an array? I have the first number (1) at the beginning, but am I failing to simply put my answer in an array? Or am I not even producing the right numbers?

I am working a question on FreeCodeCamp. I need to add a number to the end of the array, then remove the first element of the array. The function called nextInLine should return the element that was removed from it.

I'm not pletely new, but I'm not advanced in JS either. However, I'm new to algorithms. I've watched Youtube videos for assistance with Queues. I have this concept fairly understood as follows:

//array as queue (FIFO)
const arr = [];
arr.push(5); // [5]
arr.push(7); // [5,7]
arr.push(9); // [5,7,9]
console.log(arr.shift()); // [7,9]
console.log(arr.shift()); // [9]
console.log(arr.shift()); // []

However, when trying to implement this idea into my code, I seem to not maybe fully understand the presented problem in order to make it run. I am to modify the function nextInLine, and the ment that says //Display Code, I am to change that line as well.

function nextInLine(arr, item) {
  // Your code here
  arr.push(arr[0]);
  arr.shift();
  return console.log(arr, 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)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

When running the code, this is what is showing for me.

nextInLine([], 5) should return a number.
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

My function is outputting /* 2,3,4,5,1 */ but it seems to be incorrect. Perhaps I am failing to put it into an array? I have the first number (1) at the beginning, but am I failing to simply put my answer in an array? Or am I not even producing the right numbers?

Share Improve this question asked Jul 18, 2019 at 2:53 Maurice cooperMaurice cooper 591 silver badge7 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 7

You can do shift first and then push.

function nextInLine(arr, item) {
  var removedElement = arr.shift();
  arr.push(item);
  return removedElement;
}

You should be pushing item onto the array. You're simply moving the first element of the array to the end, without adding the new element.

And console.log() prints its arguments, it doesn't return them. So your function is just returning undefined.

function nextInLine(arr, item) {
  arr.push(item); // add new item to end
  return arr.length > 1 ? arr.shift() : null; // remove first item and return it
}

The conditional in the return statement prevents it from shifting off the new element if it's the only thing in the array, i.e. the array was originally empty.

You can use Array.prototype.splice to remove the first element from an array.

function nextInLine(arr, item) {
  var firstElement = arr[0];
  arr.splice(0, 1);
  arr.push(item);
  return firstElement;
}

var arr = [];
console.log('nextInLine(arr, 5) should return undefined and arr should be [5]:');
console.log('return value:', nextInLine(arr, 5));
console.log('array:', arr);
console.log('--------------------------------------')

console.log('nextInLine(arr, 1) should return 5 and arr should be [1]:');
console.log('return value:', nextInLine(arr, 1));
console.log('array:', arr);
console.log('--------------------------------------')

arr = [5,6,7,8,9];
console.log('nextInLine(arr, 10) should return 5 and arr should be [6, 7, 8, 9, 10]:');
console.log('return value:', nextInLine(arr, 10));
console.log('array:', arr);
console.log('--------------------------------------')

Update: With the help of everyone, I was not only able to solve, but understand WHY the solution was, what it was! I just had to switch around the following lines of code and that did it. Apparently they wanted you to push the item first, THEN shift on the array. Who would've knew LOL. Thanks everyone! I don't know how to close a forum post down, so I will leave it. Happy coding!

You can try this also

function nextInLine(arr, item) {
       arr.push(item);
       return arr.shift();
    }
    
    
 var arr = [1,5,6]
 console.log("removed element is : ", nextInLine(arr,10))
 console.log("array is  : ", arr)

i did fifo function with two array method - concat slice

const nextInLine = (arr, item) => item ? arr.concat(item).slice(1) : arr;

本文标签: javascriptHow to make a queue (FIFO) in JS using arraysStack Overflow