admin管理员组

文章数量:1295941

I'm currently learning Javascript at freecodecamp and am at the point where I'm learning about functions.

I'm on a task that is asking me to right a type of queue that will removed the first item from the array, and replace it with another (at the end of the array).

Here's what I have so far:

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

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

However when run with the test setup it out puts:

Before: [1, 2, 3, 4, 5]

After: [1, 2, 3, 4, 5]

I'm confused as all get out.. How would I acplish this task?

Actual task:

In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

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.

I'm currently learning Javascript at freecodecamp and am at the point where I'm learning about functions.

I'm on a task that is asking me to right a type of queue that will removed the first item from the array, and replace it with another (at the end of the array).

Here's what I have so far:

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

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

// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));

However when run with the test setup it out puts:

Before: [1, 2, 3, 4, 5]

After: [1, 2, 3, 4, 5]

I'm confused as all get out.. How would I acplish this task?

Actual task:

In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

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.

Share edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 24, 2016 at 2:28 13aal13aal 1,6741 gold badge22 silver badges47 bronze badges 3
  • Get rid of array = []; and use arr in the function since that's the one you want to modify. arr.shift(); arr.push(item) – user1106925 Commented Apr 24, 2016 at 2:32
  • The nextInLine function is modifying a local array, not the original arr, so the original testArr is unchanged when you write it to the console. – RJM Commented Apr 24, 2016 at 2:33
  • Okay so me creating a local array is what's throwing it off I would want to do what @squint said and modify the original arr – 13aal Commented Apr 24, 2016 at 2:34
Add a ment  | 

5 Answers 5

Reset to default 4

tl;dr You're using Array.prototype.shift and Array.prototype.push wrong.

shift removes the first item from an array and returns that item. Instead of

array = [];
array.shift(arr);

You want to do

var firstItem = arr.shift();

push adds an item to the end of an array. You want to mutate the original array object in place, so you want to do

arr.push(item);

Then return the first item

return firstItem;

This gives you the following function:

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

If you want to modify the passed array then you should run all the mands on it.

function nextInLine(arr, item) {
  // Your code here
  arr.shift();
  arr.push(item);
  return arr;  // this line is only required if you want to assign to a new array at the same time
}
function nextInLine(arr, item) 
{ 
    // Your code here arr.push(item); 
    return item = arr.shift(); 
    // return item; 
    // Change this line 
}

Try this:-

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

    // Your code here

    var queue = arr.push(item);
    var removeItem = arr.shift();

    return removeItem;  // Change this line
}

// Test Setup

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

// Display Code

console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
// Modify this line to test

console.log("After: " + JSON.stringify(testArr));

   

Explanation:

To add the number at the end of the array, use .push()

To remove the first element, use .shift()

To return the element that was removed, use return removeItem and after setup the var with the .shift() method.

本文标签: javascriptWriting a functions that removes an array and adds to the listStack Overflow