1

Okay so my code works fine but when a decimal i.e. 60.1, 60.2, 60.3, etc. is input for #chance it screws up profit and pay.

For example: 60% is input for chance, 1 for bet. It returns 1.65 for pay and 0.65 for profit. That's all correct.

But when I put 60.1, it returns 16.5 ( wrong decimal ) and 15.5 for profit. 16.5 seems like an easy fix but Idk how to fix it, but I have no idea why it's returning 15.5 for profit and thought maybe if I fixed pay it would fix the issue with profit.

What's wrong?

Thanks.

<script>
    $(document).ready(function(){

        function updateValues() {
            // Grab all the value just incase they're needed.
            var chance = $('#chance').val();
            var bet = $('#bet').val();
            var pay = $('#pay').val();
            var profit = $('#profit').val();

            // Calculate the new payout.
            var remainder = 101 - chance;
            pay = Math.floor((992/parseFloat((chance+0.5))) *100)/100;


            // Calculate the new profit.
            profit = bet*pay-bet;
                            profit = profit.toFixed(6);


            // Set the new input values.
            $('#chance').val(chance);
            $('#bet').val(bet);
            $('#pay').val(pay);
            $('#profit').val(profit);
        }


        parseInt($('#chance').keyup(updateValues));
        parseInt($('#bet').keyup(updateValues));
        parseInt($('#pay').keyup(updateValues));
        parseInt($('#profit').keyup(updateValues));


    });
</script>
2
  • 2
    WTH is parseInt($('#…').keyup(updateValues)) supposed to do? You're parsing jQuery wrapper objects but don't use the NaN results then. Commented Jun 27, 2013 at 0:53
  • Also, "set the new input values" - only two of them changed; two of the lines are useless. Commented Jun 27, 2013 at 0:54

3 Answers 3

2
parseFloat((chance+0.5))

looks very wrong. chance is a string, so the + operator will perform string concatenation. When you input 60, it becomes

parseFloat("60"+0.5) === 600.5

while when you input 60.1 it is

parseFloat("60.1"+0.5) === 60.1

You probably wanted

(992/(parseFloat(chance)+0.5))*100
// or
(992/parseFloat(chance)+0.5)*100
// or
992/parseFloat(chance)*100+0.5
// or something along these lines
Sign up to request clarification or add additional context in comments.

5 Comments

Wrong: puu.sh/3paY5.png Right: puu.sh/3paYU.png Is that helping you understand?
@Jake: Not really. Please tell me about the maths you are attempting to use.
Ok so I'm making a gambling site and it's a 2% house edge. So the pay should return 2 flat if 49 is entered for chance. 2% (51-49). And when they enter 60, it should give the proper X(multiplier) for what they should be paid out factoring in the house edge. Except when they enter a decimal it screws everything up.
pay = Math.floor((9900/(parseFloat(chance)+0.5))*100)/10000; This works ^. Thanks everyone. BUT, how can I make it so they can change pay and it will update chance? Thanks.
@Jake: Yeah, your maths was totally screwed up :-) To have different changing fields, you will need a different handler that does the math in reverse
1

Change parseFloat((chance+0.5)) into (parseFloat(chance)+0.5).

Actually, I can't see why it's working with 60, either. chance, as a value of a text field, is a string: "60". Strings don't add, they concatenate: "60" + 0.5 is "600.5", same as "60" + "0.5".

4 Comments

Didn't fix the problem. I did that though.
Well, that's the right thing to do: you got two independent answers saying the very same thing. If it still doesn't work, update the question and show what exactly is happening.
This is me updating it telling you I still have the same problem as posted in the OP.
And I'm telling you that your "update" is useless, because there is no way in hell that you can get the same result if you changed what @Bergi and I told you. Run this and look at the console - for all three test values, the result is changed. So while you might still have a problem, either you have a different problem, or you have applied the change wrong, but in either case you need to update your code, not say "doesn't work".
0

Try something like this:

$(document).ready(function(){
  function updateValues(){
    var chance = $('#chance').val();
    var bet = $('#bet').val();
    var pay = $('#pay').val();
    var profit = $('#profit').val();
    pay = ((992/Math.floor(+chance+0.5))/10).toFixed(2);
    profit = (bet*pay-bet).toFixed(6);
    $('#chance').val(chance);
    $('#bet').val(bet);
    $('#pay').val(pay);
    $('#profit').val(profit);
  }
  $('#chance').keyup(updateValues);
  $('#bet').keyup(updateValues);
  $('#pay').keyup(updateValues);
  $('#profit').keyup(updateValues);
});

Something is wrong with your Math.

Note:

You don't have to use parseInt() or parseFloat() to make Strings to Numbers. the + symbol in front of your String that is a Number will convert it to a Number.

See http://jsfiddle.net/PHPglue/JQJMD/ for more details.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.