codely
November 30, 2020, 2:29pm
21
Here is my updated code:
function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
testArr.shift();
return item;
// Only change code above this line
}
// Setup
var testArr = [1,2,3,4,5];
// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
Still there is 2 fails left.
nextInLine([2], 1)
should return 2
nextInLine([5,6,7,8,9], 1)
should return 5
2 Likes
codely:
testArr.shift();
You are still using testArr. Only use the parameters in the function (arr, item).
Also, this is currently empty.
codely:
shift();
We are removing an element from the beginning right? Try to incorporate one of those parameters into your answer.
Also, when you fix this line you can place the return statement here.
codely:
testArr.shift();
codely
November 30, 2020, 2:37pm
23
I don’t understand
arr.push(item);
arr.shift(arr);
return item;
sorry for ask you many things.
1 Like
codely:
arr.shift(arr);
So we are not removing an array but rather an item right?
codely
November 30, 2020, 2:39pm
25
Yes.
What’s wrong with me code
You wrote this
codely:
arr.shift(arr);
Which this part translate to I want to remove an array.
codely:
shift(arr);
Do you see it now?
codely
November 30, 2020, 2:43pm
27
jwilkins.oboe:
Do you see it now?
Yes. i see it now.
The fails say
nextInLine([], 1)
should return 1
nextInLine([2], 1)
should return 2
1 Like
codely
November 30, 2020, 2:44pm
29
function nextInLine(arr, item) {
// Only change code below this line
arr.push(item);
arr.shift(arr);
return item;
// Only change code above this line
}
// Setup
var testArr = [1,2,3,4,5];
// Display code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6));
console.log("After: " + JSON.stringify(testArr));
Actually, I am sorry this part was correct
codely:
shift();
I’m a dummy shift already removes the first element from the beginning of an array. Sorry about that. Must be tired.
The test still passes if you pass through a parameter. So I guess it is very forgiving.
But you do need to return the second line
1 Like
codely:
arr.shift(arr);
I am talk about this line.
You had it right with just arr.shift()
You just have to change the return statement. Right now you are returning item when you need to return the element that was removed.
Hint:
arr.shift()
Your answer only needs to be two lines of code not three
Were you able to pass the challenge?
codely
November 30, 2020, 3:13pm
34
It is Night 8:42 PM Here
So I will try it tomorrow.
Well you are super close. You just have to add the return statement in front of here.
arr.shift()
and delete this line
return item
1 Like
codely
November 30, 2020, 3:15pm
36
Hey @jwilkins.oboe !
OMG!
I passed the test.
I did as you said in
jwilkins.oboe:
arr.shift()
and delete this line
return item
return arr.shift();
Thanks all for your Guides
5 Likes
smc
November 30, 2020, 4:46pm
37
Congratulations
But the problem is to understand what is happening instead of only pass the exercise. If you have a function more complicated you need to know what shift() is doing. In my opinion, for practice and understanding what is happening i would return a stored variable. Even if many times you can use less code, for readability or reusability you need to do a little more.
3 Likes
codely
November 30, 2020, 5:13pm
38
Hey @smc !
Thanks for your advice.
But I already understood the problem