Learning JS and stuck!

Hello,

I am learning basic JS and I am stuck on the Stand In Line section. I’m not sure if I missed something or what but I don’t really understand how to work the problem out. I figured that I would use the .pop and .shift functions, but it just return an error of TypeError: Object doesn’t support property or method ‘pop’. So I am now very confused and any help would be amazing. Thanks!
Dale

Hi Dale,

Could you please provide the code you have written ?

Hello Dale,

I believe you are calling pop method or change in a non-array object, send your code so we can help you.

So after messing with some of the earlier lessons to see what I was missing, I see that I should probably be using push and not pop, so I changed that. Of coarse it did not correct the issue, but just wanted to explain the push instead of pop.

function nextInLine(arr, item) {
// Your code here
nextInLine.push();// What I added.
nextInLine.shift();//What I added.
return nextInLine.shift();// 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));

push() and shift() must be used on arrays.Example :

// Example 1
var arr = [1,2,3];
arr.push(4); //=> good use of push() . arr is an array
console.log(arr); //=> [1,2,3,4]

// Example 2
function someFunction() {
    // some code
}

someFunction.push(6); // Wrong. someFunction is not an array, it's a function.

In your code, you are using push() and shift() on nextInLine, which is a function.

Hi Dale
I’m looking at your code, below my code with the explanations.

Method shift removes the first element from an array
Method pop remove the last element in array
Method push insert the element in last position in array

function nextInLine(arr, item) {
nextInLine.push(item);// method push insert element in last position
//nextInLine.shift();// method shift remove last element in array
return nextInLine;
}

// Test 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));

Let me know if this was really what you needed to understand.

1 Like

console.log(nextInLine(testArr, 6)); // Modify this line to test
ok you got the start right … so next
it wants you to add the item in your case is the number 6 to your testArr
but you are trying to add it to a function
remember a function in basic terms is just a list of things to do
so what do you want to do … add item to your testArr … how do you add a item to a array well if you werent using a function you write testArr.push(6)
so if your function is a list of things to do
frist thing is add item to arr … arr is a function paramater and represents whatever you sent when you called your function …console.log(nextInLine(testArr, 6))
next you need a variable to store what you take out of the array
eg var result
and how do you remove the first item from your array well if you werent using a function you would … you got it … testArr.shift()
so if you werent using a function you would then write result = testArr.(shift)
and to see it you would console.log result
but you have to do this with a function and then return result
now i wrote this in such a way as not to write the code for you but if you look at it you should hopefully be able to figure out the answer as its all there in a roundabout way

Thank you everyone! I got it after a little playing with the code and reading up on stuff. Thanks again!