codely
November 29, 2020, 6:11am
1
Tell us what’s happening:
I am not able to understand what to do.
Your code so far
function nextInLine(arr, item) {
// Only change code below this line
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));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0
.
Challenge: Stand in Line
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
Hey @codely !
What part of the instructions are giving you trouble?
1 Like
1. Add the number to the end of the array, then remove the first element of the array.
So you have to do two things:
add the number passed into the function to the end of the array passed into the function
remove the first element from the array passed in
2. The nextInLine function should then return the element that was removed.
In the function’s return statement you return the element that was removed from the front of the array.
2 Likes
codely
November 29, 2020, 6:17am
4
Hey @jwilkins.oboe !
I can’t understand the whole question.
2 Likes
codely
November 29, 2020, 6:18am
5
Thanks @bbsmooth For giving hints.
I think i have redo the whole JS basic , i can’t understand anything in JS.
1 Like
Let’s address @bbsmooth ’s first line.
How to add a number to the end of the array. There is helpful method that you can use that was introduced in an earlier lesson.
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
How to remove the first element of the array.
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
codely
November 29, 2020, 6:21am
7
As i said before i have redo all the JS basic.
I am not able to understand anything in JS even after i learn C Lang.
Well it’s normal to struggle at first when you are learn a programming language.
I would suggest looking at the first link I shared. How can you apply that method to arr?
How is that going to help you add a number at the end of the array?
1 Like
codely
November 29, 2020, 6:25am
9
First i will Learn the All that i learned before.
And thanks @jwilkins.oboe and @bbsmooth for your guide.
1 Like
Well if you want to go through the whole section again you can.
But for this problem you only need to use the two methods that I linked to above.
The solution only needs to be two lines of code.
1 Like
codely
November 29, 2020, 1:05pm
11
After doing all the JS basics.
Here is my updated code. Still, it didn’t pass
function nextInLine(arr, item) {
// Only change code below this line
testArr.push(6);
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));
1 Like
ILM
November 29, 2020, 1:10pm
12
what does the challenge asks you to return?
also, to make your function reusable you must use the function parameters
1 Like
codely
November 29, 2020, 1:18pm
13
return the element that was removed.
codely:
(arr, item)
These?
bedward
November 29, 2020, 1:20pm
14
Here is a link to the hint
, there is a solution there, but i think its better to try to solve the problem yourself first…
Stand in Line
Hints
Hint 1
The push() method adds an item to the end of an array.
Hint 2
The shift() method removes the first element of an array. It also returns the element removed.
Hint 3
The function nextInLine uses arr and item. Those are what the tests will use to pass the array elements they will test with. It allows the function to be reusable. Do not hardcode any of the tests inside the function.
Solutions
Solution 1 (Click to Show/Hide) function nextInLine(arr, item) {
// …
Good luck !
1 Like
ILM
November 29, 2020, 1:21pm
15
are you returning the element that was removed?
also, yes those re the function parameters
1 Like
codely
November 29, 2020, 1:22pm
16
No.
and i don’t know which element to return
Thank @bedward for the hints
1 Like
ILM
November 29, 2020, 2:07pm
17
how are you removing the first element from the array?
1 Like
We are making good progress.
codely:
testArr.
So hardcoding testArr only makes our function work for that particular array. It would be nice to re use this function for other arrays. Let’s see if we can put one of those parameters (arr, item) to good use.
codely:
testArr.push(6);
Instead of hardcoding 6 let’s see if we can use those parameters that were mentioned earlier. If you reread the problem the you will know which parameter (arr or item) makes sense to add in those parenthesis.
codely:
testArr.shift;
You have a syntax error here. Please refer to the link on shift that I gave you earlier to add what is missing at the end. Also remember that I said this problem could be solved with just two lines of code. So maybe we could bump that return up to this line.
4 Likes
You are doing good and getting close. Remember a functions parameters are kind of like local variables that only exist in that function.
The array is created for you and the function is called for you at the bottom. They also already passed the values you need into your function.
I drew some arrows to try to help you understand what is getting passed where.
All you need to do is reused the values at the top inside your function code block.
5 Likes
you need a proper understanding of shift()
for this, you did the push part ok.
the shift()
method:
removes the first element from an array and returns that removed element. This method changes the length of the array.
Return value
The removed element from the array;
1 Like