I SEE NOTHING WRONG? Access Multi-Dimensional Arrays With Indexes

Hello,
I see nothing wrong with my JS code below. Why do I keep getting various ERROR MESSAGES?
One is below.

Thanks!
UM

// Setup

var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

// Only change code below this line.

var myData = 7 + 1;

var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

myData = myArray[2][1];

ERROR MESSAGE =

// running tests

You should be using bracket notation to read the correct value from myArray.

// tests completed

And just so you know, the ZERO (0) position didn’t / doesn’t work anymore either in JS ARRAYs like this.
BUG needs to be fixed.

What is the var myData = 7 + 1 line for? And, why are you redefining myArray?
Your solution is right if you get rid of the extra definitions.

You only need to change myArray in the starting code that they give you to access the indexes requested. This should be at most 2 character changes.

1 Like

When I get rid of all of the EXTRA definitions, as you feel I need to do,
and leave JUST THIS code, see ERROR below
Any suggestions appreciated.

// Setup

var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13, 14]];

// Only change code below this line.

myArray[2][1];

I get the following ERROR

// running tests

myData is not defined

myData is not defined

// tests completed

All you have to do is change the line of code that was given to you :slight_smile:, nothing more. I hope that helps

But that is what I did. My line of code has been changed to ALL of the following,
and nothing works:

myArray[2][1];
myData[2][1];
myData = myArray[2][1];

Any other suggestions?

Thanks,
UM

I found the solution. The problem was that MOST ALL JavaScript Array Problems to this point had NOT used “VAR” to read the value from the setup arrays. This one did.

If a variable was not used before, you need to declare it
So you need to declare myData before being able to use it
It seems that previously you just hadn’t encountered a variable that you needed to declare because it was already declared for you

Nothing was declared. The program was sending me around in circles in CONFUSION.
Like it is now with this one too! There is NO ANSWER that I can come up with without getting
MULTIPLE ERRORS . . .

Any ideas??
Thanks

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

This is for Manipulatiing ARRAYS w/ POP
ERROR was for above. I used the HINT which was USELESS.

SyntaxError: unknown: Unexpected token, expected , (7:27)
5 |
6 | // Setup

7 | var myArray = [[“John”, 23];
| ^
8 |
9 | // Only change code below this line.
10 | var removedFromMyArray, myArray.pop[“cat”, 2];

In this line you have two opening square parens and only one closing one - please don’t change the original setup, if you change the setup you can’t pass the tests

Here you are not using assignment operator =, and pop() requires round parens, plus it doesn’t require an argument
Please use the assignment operator = to assign value of myArray.pop() to variable removedFromMyArray

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 @ilenia 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.

1 Like

Thank you all. The solution was MUCH MUCH simpler than the original errors made it out to be.
The example in the POP problem is all one needs to solve.

Thank you again.
UM