So your code:
// Example
var ourArray = [1,2,3];
var removedFromOurArray = ourArray.pop();
// removedFromOurArray now equals 3, and ourArray now equals [1,2]
// Setup
var myArray = [[“John”, 23];
// Only change code below this line.
var removedFromMyArray, myArray.pop[“cat”, 2];
var removedFromMyArray, myArray.pop();
The sample code maybe isn’t commented as well as it could be, but the way that it works is pretty straightforward. An array is declared (with the var
keyword), and given a value: var ourArray = [1,2,3];
The var
is required, in order to keep the variable in context.
Now, with that being an array, it automatically inherits a number of Array functions or methods: .push()
, .pop()
, .shift()
, .unshift()
, .reverse()
etc etc. These are all functions you get with an Array-type object. You also get some properties, like .length
. The difference between the property .length
and the method .pop()
is… the trailing parenthesis. A function needs those. A property doesn’t know what to do with them, but a method (being a function) requires them.
So, in the second line of the sample code, they use an array function: ourArray.pop()
. If you take a look on the Mozilla Developer site, you will see what that function does: it takes the LAST value of an array, removes it from the array, and returns it.
Returns it? What does that mean? It means, when the function is run, a value is sent BACK from that function. In this case, it’s the value we removed. And we can take that returned value, and (as the sample does) assign it to a variable: var removedFromOurArray = ourArray.pop();
So in short, we define a variable, var removedFromOurArray
, and immediately assign it a value. Not a simple primitive value like 5
or "The quick brown fox"
, but “Whatever the value returned from ourArray.pop()
might be.” In this particular case, that value is 3
, the last value in ourArray
.
So ourArray
has been modified in place. This is an important concept, we have modified the original array, and returned something FROM that modification.
Now, looking at your portion of the code:
// Setup
var myArray = [[“John”, 23];
// Only change code below this line.
var removedFromMyArray, myArray.pop[“cat”, 2];
var removedFromMyArray, myArray.pop();
The error you’re getting is on the line where myArray
is being declared and assigned. The error, "UnexpectedToken, expected ,"
, is telling you that something is wrong on this line. As @ILM has suggested, the issue is in how the array has been redefined: you have two opening angle brackets (the [[
at the beginning of the assignment part), and only one closing bracket. So in essence, when this line was changed, it says "create a variable named myArray, and assign to it an open-ended array containing a nested array and… um… " and closes the statement without actually finishing the sentence.
This line falls above the // Only change code below this line
line, so it did not need to (and should not have been) modified. Had it been left alone, then you would have gone on to your second line, var removedFromMyArray, myArray.pop["cat", 2];
which not only confuses the heck out of the javascript engine, it confuses the heck out of ME.
In this line, you want to declare the var removedFromMyArray
which you do, but then you do something else: you try to declare a property on myArray? A property named pop
, which you seem to treat as an array, or maybe an object, and then you throw in a two? I don’t know. The logic escapes me.
Instead, simply declare the variable, var removedFromMyArray
, and assign it a value var removedFromMyArray = myArray.?????????;
All those question marks above? That should be where you call the .pop()
method on your myArray
, which removes the last value from the array, and returns it. In this case, it will return it and store it to the variable removedFromMyArray
.