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);
Because arrays a 0-indexed, and location["ranger"+i].station
is likely 1-indexed. location.weaponBulbs
is an array, so location.weaponBulbs[1]
actually represents the second element in the array, and similarly location.weaponBulbs[10]
accesses the eleventh element.
By subtracting one from the integer location['ranger'+i].station
, it will correctly access the relevant array value.
location.weaponBulbs
appears to be a two-dimensional array.
location["ranger"+i].station-1
yields an integer, so the end result is
location.weaponBulbs[n][0]
, where n
is the value of location["ranger"+i].station-1
It is selecting weapons from the superBlinders
array.
Since the ranger numbers start from 1 up to 3, and the index of the array starts from 0 up to 2, you need to subtract 1 from the ranger numbers in order to directly map them to the weapons.
"ranger"+i
results in a string. location.weaponBulbs
is an array here - so possibly location looks like this...
var location = { "weaponBulbs":superBlinders, /* this is an array again */ 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} ... }
location["ranger"+i].station
would become an int
in this
Finally for each station
, it is lighting up a random weaponBulb
intensity...
Whow I learned how to write a rock in javascript !!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.