0

I have a function that randomly selects a monster from an object. Here's the function:

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() {
                        var monsterArray = Object.keys(monsters);   
                        var randomKey = Math.floor(Math.random() * monsterArray.length);
                        return $("<p>Holy Crap!  There's a " + monsterArray[randomKey] + " in here!</p>").properDisplay();  
                    }
                    pickRand();

                }
        }
    };

Here's the object:

var monsters = {
    zombie:  {
        hitPoints: 10,
        loot: "magic knife"
    },
    skeleton: {
        hitPoints: 15,
        loot: "magic shield"
    },
    ghoul:  {
        hitPoints: 12,
        loot: "magic helm"
    }
};  

It's set up to randomly select "Zombie", "Skeleton", or "Ghoul." Everything works fine. How do I take whatever was randomly selected and save it to a variable?

I've tried a couple things like:

var beast = pickRand();

and

var beast = monsterArray;

But no luck. What am I missing?

4
  • What does properDisplay return? Commented Nov 28, 2015 at 20:16
  • it adds some CSS and prepends some text in the game. It's more cosmetic than anything. Commented Nov 28, 2015 at 20:23
  • Your pickRand function will be returning the return value of properDisplay. Why not just return monsterArray[randomKey] Commented Nov 28, 2015 at 20:26
  • If I do a return, how do I call it later? Sorry, still trying to learn JS. What I'm trying to do is assign the random value to a variable so I can use it later when you decide to fight the monster. I want to be able to call whichever monster was randomly generated by name. (zombie, ghoul, etc). Commented Nov 28, 2015 at 20:33

2 Answers 2

1

It looks like the problem is that you're returning the return value of the properDisplay function.

If you used an array of monsters rather than a map you could store all of the monster information when you select a random one:

var monsters = [
    {
        name: 'zombie',
        hitPoints: 10,
        loot: "magic knife"
    },
    {
        name: 'skeleton',
        hitPoints: 15,
        loot: "magic shield"
    },
    {
        name: 'ghoul',
        hitPoints: 12,
        loot: "magic helm"
    }
];

function pickRand(arr) {
    var index = Math.floor(Math.random() * arr.length);
    return arr[index];
}

var monster = pickRand(monsters);

Now that you have your monster you could display it:

$("<p>Holy Crap!  There's a " + monster.name + " in here!</p>").properDisplay();

Demo: https://jsfiddle.net/louisbros/3cu9spfd/

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

6 Comments

Here is the fiddle I have @louisbros. jsfiddle.net/eut1h5m7 When I call the variable, it says [object object]. The only thing I can think is I placed the variable incorrectly? The monster is called when you "go east" at the beginning of the game.
my bad. I missed the display of monster.name. Thanks @louisbros. All set!
Is there any way to have the variable remember the same randomly selected monster for a period of time? ie. it randomly selects Zombie - how do I have it remember Zombie until I want it to randomly generate another monster? Does that make sense? I get that everytime the function is called, it'll randomly generate again. I need it to temporarily remember it's selection.
Yes you can keep the value each time by giving the monster variable a greater scope. For example, declare the variable outside the function and set it inside the function.
OK, I understand declaring outside the function. You do that in your example. I'm vague on "set it inside the function" I did this: function pickRand(arr) { var index = Math.floor(Math.random() * monsters.length); return arr[index]; var monster = pickRand(monsters); }; var monster = pickRand(monsters); But no luck. I'm sure it's the inside the function part I'm messing up.
|
0

var beast = pickRand(); should totally work. You just need to assign the variable right when you call it?

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() {
                        var monsterArray = Object.keys(monsters);   
                        var randomKey = Math.floor(Math.random() * monsterArray.length);
                        return $("<p>Holy Crap!  There's a " + monsterArray[randomKey] + " in here!</p>").properDisplay();  
                    }
                    var beast = pickRand();
                }
        }
    }

2 Comments

Here's why I thought it didn't work. I used that and just to see if it would call correctly, I added alert(beast); and it outputs objectObject. I'm guessing that means it's calling the object incorrectly...
maybe try alert(beast[0])? You may need to look at the first item in that object

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.