I have an object that looks like:
var monsters = {
zombies: {
name: "zombie",
hitPoints: 10,
loot: "magic knife"
},
skeleton: {
name: "skeleton",
hitPoints: 15,
loot: "magic shield"
},
ghoul: {
name: "ghoul",
hitPoints: 12,
loot: "magic helm"
}
};
I'm trying to set a function that will randomly select one of the properties in the variable. (zombies, skeleton, ghoul)
Here's what I have:
var travel = function(direction) {
var newRoom = rooms[currentRoom.paths[direction]];
if (!newRoom) {
$("<p>You can't go that way.</p>").properDisplay();
}
else {
currentRoom = newRoom;
$("<p>You are now in the " + currentRoom.name + " Room.</p>").properDisplay();
if (currentRoom.hasMonsters) {
function pickRand(monsters) {
var result;
var count = 0;
for (var prop in monsters)
if (Math.random() < 1/++count)
result = prop;
return $("<p>Holy Crap! There's a" + result + "in here!</p>").properDisplay();
}
}
else {
$("<p>Whew! Nothing here.</p>").properDisplay();
}
}
};
Note: The hasMonsters is in a separate object. It determines if a specific room has a monster or not.
How can I randomly select one of the monsters and insert in the output? I'm guessing I'm calling the object incorrectly and that's why it's not working.