-1

Lets say that i have a variable which has a value of 5, the higher the value of the variable the higher the chance of succes.

So how could i do something like this?:

function mission1() {
if (user_power < 5) {
//30% change of succes
} else if (user_power == 5) {
//50% change of succes
} else if (user_power > 5) {
//80% change of succes
}
SpringerJerry
  • 178
  • 1
  • 8
  • 2
    Does this answer your question? [Percentage chance of saying something?](https://stackoverflow.com/questions/11552158/percentage-chance-of-saying-something) – Ivar Feb 18 '21 at 18:32

1 Answers1

1

I don't know if I understood correctly what you whish for but here is my solution:

  function mission1(user_power) {
    if (user_power < 5) {
      // 30% chance of succes
      return Math.random() < 0.3;
    } else if (user_power == 5) {
      //50% chance of succes
      return Math.random() < 0.5;
    } else if (user_power > 5) {
      //80% chance of succes
      return Math.random() < 0.8;
    }
  }

The function returns a boolean value based on the parameter given.
Note: Math.random() returns a floating point number between 0 and 1, so the possibility it's lower than 0.8 is 80%.

Loránd Péter
  • 184
  • 1
  • 5