Ask coding questions

← Back to all posts
math.random Javascript
Keegooo (6)

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

Comments
hotnewtop
SPQR (595)

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

SPQR (595)

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.

JackFly26 (111)

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