10.Array Positions ERROR when there shouldn't be one?


#1

No matter what I try I get some kind of error....

var junkData = ["Eddie Murphy",49,"peanuts",31];
console.log = (junkData[3]);

This DOES print out 31... but I'm getting the error message "Oops, try again. Alas, you should have printed 31, as that is the 4th element!"

I also tried taking the () around junkData off completely, still printed 31, still got same error.

var junkData = ["Eddie Murphy",49,"peanuts",31];
console.log(junkData[3]);

This one doesn't work at all. It doesn't make anything but error messages with this.

Am I missing something simple? Please help.


#2

If it's asking for the 3rd element that would be junkData[2].

Can you post the context of the question?


#3

Array positions
It's nice that we can put a list of data into an array. But now we need to learn how to get access to the data inside the array.

The position of things in arrays is fixed. So we just need to know the array name (here, it is junkData), and the position of the data we want, and we're done.

Small complication: the position (or the index) of each bit of data is counted starting from 0, not 1.

First element in the array: junkData[0]
Third element in the array: junkData[2]
Arrays have 0-based indexing, so we start counting the positions from 0.

Print out the fourth element of the array.
1. Start with figuring out how to express what the fourth element in the array is.
2. Then use console.log() to print things out!

It's definitely asking for the one that is printing out.


#4

One way to think of array indices is as offsets. How far is the element from the left edge?

first element offset => 0

last element offset => len(list) - 1

#5

I copied this and it worked. Maybe try this one again and see if it works now.


#6

Yea I reset the browser today again and now it works. Weird guess it was just a glitch. :stuck_out_tongue:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.