Skip to content
← Back to Community
math.random Javascript
Profile icon
Keegooo

Every time I do the function it always results in a chickenCount = 0. I've tried multiple ways to fix this but can't pinpoint it. Help would be greatly appreciated.

gamble.onclick = function() { if (chickenCount >= 1) { gamble = (Math.random()); console.log(gamble) if (gamble >= 0.5); (chickenCount = chickenCount * 2); if (gamble < 0.5); (chickenCount = 0); counter.innerHTML = chickenCount; gambled++; gamble.innerHTML = gamble;

https://repl.it/@Keegooo/click

Voters
Profile icon
mHmdHmd33
Profile icon
Ozy12
Profile icon
Keegooo
Comments
hotnewtop
Profile icon
SPQR

Essentially it's just going down the list, it doesn't actually associate your if statements with your chickenCount modifiers.

Profile icon
SPQR

I'm not entirely familiar with JS, but the problem that stands out to me is that you don't have the chickenCount = 0 inside curly braces {}. So, the program isn't actually doing chickenCount = 0 only if gamble < 0.5, it's multiplying it by 2 and then setting it to zero every time regardless of gamble's value.

Profile icon
JackFly26

I think I fixed it at https://repl.it/@ThatSmart/click

Profile icon
JackFly26

It looks like you are using 2 different variables called gamble. Did you mean to make the random number gambled?