Skip to content
Advertisement

How Can I Assign A Specific Value To Random Numbers

Hello I’m fairly new to JavaScript, in fact I don’t know really anything about JavaScript. I’m trying to make a tool for a game where you click a button to generate a random number than you have a balance where it goes up a specific amount based on a range of numbers you rolled. So let’s say 1-10 is worth $1, 11-20 is worth $2, and 21-30 is worth $3. Maybe you roll a 26, 14, 18, and a 8. That would be worth $8. My issue right now is assigning a different value to a specific range of numbers and than making the balance go up by the amount related to the number rolled. I’m not looking for a specific answer. I don’t want handouts and I don’t want to come here to find someone to do the work for me for free. But if someone could maybe help me out in figuring it out or point me to a place where i could find a solution than that would be very appreciated. Here is what I have so far that is far away from where it needs to be.

JavaScript
JavaScript

Advertisement

Answer

Assuming the value isn’t directly computable from the number via straight arithmetic (see Pointy’s comment), you could define an array of “buckets”, where each entry has a minimum, a maximum, and a value, and then find the bucket for any given value:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement