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
brandonlind2
7,823 Pointsdoes 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
Michael Hulet
47,913 PointsMath.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;
Jennifer Nordell
Treehouse TeacherMichael 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.
Michael Hulet
47,913 PointsNothing 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
brandonlind2
7,823 Pointsbrandonlind2
7,823 Pointsthanks man!!! I'm surprised I didn't see that lol