2

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 completely 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 comment 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?

6 Answers 6

7

You can do shift first and then push.

function nextInLine(arr, item) {
  var removedElement = arr.shift();
  arr.push(item);
  return removedElement;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I see, you're creating a variable named 'removeElement' and assigning it the literal 'removed element', and you're also returning that. As well as pushing whatever 'item' you decide to push to the end. I seem to understand what I was missing, which was assign the shift method to a variable, then returning said variable. Thank you for the input @Samuel
1

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.

1 Comment

Oh right! I completely forgot that console.log(); does not return anything and comes out as undefined. It will log the results but does not return it back to the function? If I said that correctly.
1

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('--------------------------------------')

1 Comment

Awesome! Thank you for the input @pindev. I appreciate your response.
1

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!

Comments

0

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)

Comments

0

i did fifo function with two array method - concat slice

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.