Vodka_Tonic
Vodka_Tonic

Reputation: 152

I'm not understanding what's going on in this javascript object

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

Upvotes: -1

Views: 83

Answers (4)

Siva Tumma
Siva Tumma

Reputation: 1701

  1. "ranger"+i results in a string.
  2. 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}
    ...
    }
    
  3. location["ranger"+i].station would become an int in this

  4. Finally for each station, it is lighting up a random weaponBulb intensity...

Whow I learned how to write a rock in javascript !!

Upvotes: 0

Jon
Jon

Reputation: 10918

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.

Upvotes: 0

BenM
BenM

Reputation: 53246

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.

Upvotes: 1

Tieson T.
Tieson T.

Reputation: 21246

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

Upvotes: 0

Related Questions