0

Should I just change payback and payback to payback1 and payback2 ? Or is there better way to do it?

function play() {
    var bet = document.getElementById("bet").value;
    var pips = Math.round((Math.random()*5)+1);

    if(pips == 1|| pips == 3 || pips == 5){
      document.getElementById("answer").innerHTML = "No pay";
    }
    else if(pips == 2|| pips == 4) {
      var payback = 1.25*bet;
      document.getElementById("answer").innerHTML = "You'll receive: "+payback;

    }
    else {
      var payback = 1.50*bet;
      document.getElementById("answer").innerHTML = "You'll receive: "+payback;
    }
}

Line 15: 'payback' is already defined.

I seriously tried coming up with a sensible title...

5
  • what do you do with payback? Oct 28, 2015 at 17:12
  • It was a typo. I replaced bet with payback.
    – Skege
    Oct 28, 2015 at 17:13
  • 1
    All variables created using var will be hoisted to the closest function definition. This means that your payback function is only defined once but set in two locations. As long as the value of payback is ok to be changed, you can stick with using the single variable. It would be better to declare it at the top of the function though. Oct 28, 2015 at 17:13
  • Yes! Now I have my answer. Thank you so much!
    – Skege
    Oct 28, 2015 at 17:16
  • @JaakkoUusitalo if you have solved your problem, feel free to accept one of the answers below
    – juvian
    Oct 28, 2015 at 17:37

3 Answers 3

2

You can just declare it at the beginning and then modify it on your if/else:

function play() {
var bet = document.getElementById("bet").value;
var pips = Math.round((Math.random()*5)+1);
var payback;

if(pips == 1|| pips == 3 || pips == 5){
  document.getElementById("answer").innerHTML = "No pay";
}
else if(pips == 2|| pips == 4) {
  payback = 1.25*bet;
  document.getElementById("answer").innerHTML = "You'll receive: "+bet;

}
else {
  payback = 1.50*bet;
  document.getElementById("answer").innerHTML = "You'll receive: "+bet;
}

}

1
  • @JaakkoUusitalo and just in case I would use var bet = Number(document.getElementById("bet").value);
    – juvian
    Oct 28, 2015 at 17:16
1

Beside the declaration problem, is it adorable to get a not normalised distribution for your bet program? Your 1 and 6 does not have the same chance like the other numbers. The chance is only 1/2 of the rest.

Please see distribution in your case and some more better distributed values.

var i, count = {}, v;
for (i = 0; i < 12000; i++) {			
    v = Math.round((Math.random() * 5) + 1);
    count[v] = (count[v] || 0) + 1;
}
document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');
count = {};
for (i = 0; i < 12000; i++) {			
    v = Math.floor((Math.random() * 6) + 1);
    count[v] = (count[v] || 0) + 1;
}
document.write('<pre>' + JSON.stringify(count, 0, 4) + '</pre>');

0

You should be able to do it inline and skip the variable declaration:

document.getElementById("answer").innerHTML = "You'll receive: "+(1.25*bet);
1
  • Of course. It seems that there are multiple ways to do it! Thank you!
    – Skege
    Oct 28, 2015 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.