Reputation: 23
I have these If statements based off of a Math.random() function and it works for the most part but sometimes nothing evaluates to true
I've tried changing the numbers that the Boolean statements compare too and I've tried changing the number that Math.random is multiplied by
var dodge = {
dodgeValue: function() {
return Math.floor(Math.random() * 10) + 1
},
roll: function() {
console.log('ROLL AFTER DODGE?'); //DELETE THIS
if (this.dodgeValue() <= 5) {
console.log('Ball Rolls to Person One');
dodgeBall.ball.position = 'on ground';
dodgeBall.personOne.ball();
}
if (this.dodgeValue() >= 5) {
console.log('Ball Rolls to Person Two');
dodgeBall.ball.position = 'on ground';
dodgeBall.personTwo.ball();
}
},
This is one of the two parts that have the same problem the other one when copied and pasted was really jumbled so I left it out but if anyone had any ideas on why this is happening or how to fix it that would be great.
Upvotes: 1
Views: 59
Reputation:
That's because you call dodgeValue twice, which calls random twice.
Ignoring the fact that random numbers in computers are actually psudeorandom for a second, let's look at what happens when you call dodgeValue twice to get two random numbers like you have.
You could conceivably get the following numbers from this code:
In the first case, the first if will be true and execute, since 4 <= 5
.
The first if will be false, but the second if will be true, since 6 > 5 (first if is false)
and 8 >= 5 (second if is true)
.
Now, what happens in that third case? Both if's will be false, since 6 > 5 (first if is false)
and 2 < 5 (second if is false)
.
If that is how you intend for it to work (which I'm assuming not), keep your code as is. If not, you have 2 options depending on your needs.
If you need to store the output of dodgeValue for later, you can use a variable:
var dodge = {
dodgeValue: function() {
return Math.floor(Math.random() * 10) + 1
},
roll: function() {
console.log('ROLL AFTER DODGE?'); //DELETE THIS
var val = this.dodgeValue();
if (val <= 5) {
console.log('Ball Rolls to Person One');
dodgeBall.ball.position = 'on ground';
dodgeBall.personOne.ball();
}
if (val >= 5) {
console.log('Ball Rolls to Person Two');
dodgeBall.ball.position = 'on ground';
dodgeBall.personTwo.ball();
}
},
If you just need them to be opposites of each other, then just use an else clause instead.
var dodge = {
dodgeValue: function() {
return Math.floor(Math.random() * 10) + 1
},
roll: function() {
console.log('ROLL AFTER DODGE?'); //DELETE THIS
if (this.dodgeValue() <= 5) {
console.log('Ball Rolls to Person One');
dodgeBall.ball.position = 'on ground';
dodgeBall.personOne.ball();
} else {
console.log('Ball Rolls to Person Two');
dodgeBall.ball.position = 'on ground';
dodgeBall.personTwo.ball();
}
},
Upvotes: 0
Reputation: 324810
You're generating two random numbers. A new one each time you write this.dodgeValue()
.
Additionally, if the value is exactly 5
then both branches will run - this probably isn't intended behaviour.
You should do something like let dodge = this.dodgeValue();
and then use if( dodge <= 5)
. Additionally, you should use else
rather than an "if A then ... if not A then ..."
Upvotes: 4