I'm working on some of codeschool's javascript exercises and this part has me baffled. In the function dontPanic, I'm not understanding what's going on at the end of this statement
location.weaponBulbs[location["ranger"+i].station-1][0].
Basically, the part where it says station-1. I'm not sure why it's subtracting 1 from the station object. I also don't understand the [0] after station-1. Which object or array is [0] referring to?
My guess was the [0] after station-1. is the assigned number for station but I really need some clarification. I don't want to continue on with this lesson without understanding this. I put a comment above the line I need help understanding.
var superBlinders = [ ["Firestorm", 4000], ["Solar Death Ray", 6000], ["Supernova", 12000] ];
var lighthouseRock = {
gateClosed: true,
weaponBulbs: superBlinders,
capacity: 30,
secretPassageTo: "Underwater Outpost",
numRangers: 3,
ranger1: {name: "Nick Walsh", skillz: "magnification burn", station: 2},
ranger2: {name: "Drew Barontini", skillz: "uppercut launch", station: 3},
ranger3: {name: "Christine Wong", skillz: "bomb defusing", station: 1}
};
function dontPanic (location){
var list = "";
for(var i = 1; i<=location.numRangers; i++){
//this is what I don't understand
list = list + location["ranger" + i].name + ", man the " +
location.weaponBulbs[location["ranger"+i].station-1][0] +
"!\n";
}
alert("Avast, me hearties!\n" +
"There be Pirates nearby! Stations!\n" + list);
}
dontPanic(lighthouseRock);
location.weaponBulbs[( (location[ ("ranger"+i) ].station) - 1)][0]stationis one-indexed, but arrays are zero-indexed. You have to subtract one when going from station numbers to elements of an array. As for the[0],weaponBulbsis an array of arrays.