-1

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);
2
  • 1
    it's actually location.weaponBulbs[( (location[ ("ranger"+i) ].station) - 1)][0] Commented Jan 12, 2014 at 10:46
  • station is 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], weaponBulbs is an array of arrays. Commented Jan 12, 2014 at 10:46

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0
  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 !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.