Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

does anyone know why the random number generating function leveling isn't working?

var player={
    name: ' ',
    level: 1,
    health: 100,
    attributes:[],
    skills:[],
    inventory: [],
    equiped:{ 
         weapon: '',
         armor:''
        },
    armor: 0,
    money: 0,
    invetoryAdd: function(add){
    this.inventory.push(add);
    },
        inventoryDrop: function(drop){
    var dropIndex= this.inventory.indexOf(drop);
    this.inventory.splice(dropIndex,1);
    }
};

function leveling(){
    var max=player.level + 5;
    var min=player.level - 5;
    if (min <= 0) {
         min= 1;
        }
    var level= Math.floor(Math.random() + (max - min)) + 1 + min;
    return level;
    }

2 Answers

Math.random gives back a pseudorandom number between 0 and 1. It's most commonly multiplied to make a range of values that a number could be. In your example, you're adding it to something else, but I bet you meant to multiply it. Try this:

//Notice how we're multiplying now, instead of adding
var level = Math.floor(Math.random() * (max - min)) + 1 + min;

thanks man!!! I'm surprised I didn't see that lol

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Michael is correct. That should be a multiplication in there. But I'd like to point out that I don't see anywhere in your code where you're actually calling the leveling function. Maybe it's in a bit of code that you didn't paste. So currently (to me) it looks like you're returning your level variable to nowhere.

Nothing at all is actually getting called in the code he pasted. If you notice, everything else is just declaring and defining the player object. All the calling goes on somewhere else that wasn't uploaded