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:
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 3In 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.
-
Get rid of
array = [];
and usearr
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 localarray
, not the originalarr
, so the originaltestArr
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 originalarr
– 13aal Commented Apr 24, 2016 at 2:34
5 Answers
Reset to default 4tl;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
版权声明:本文标题:javascript - Writing a functions that removes an array and adds to the list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741625901a2389079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论