Announcement

Collapse
No announcement yet.

Anyone willing to offer code help?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Anyone willing to offer code help?

    I know we have a few programmers and web designers on here, and I was wondering if any of you would be willing to help me out with a small Javascript project that's not functioning properly.

    Here's the page in question.

    It's meant to calculate dice probabilities for an RPG system I've been working on. It's a pretty simple calculation because each die always has a 50/50 chance of success, and the only thing that matters is counting successes. However, any time the "number of dice" input is above 10, and the "number of successes needed" input is anything other than 1, 0, or equal to the dice (which all have special cases plugged in), it returns 0.

    For example, if you tell it you need 3 successes on 10 dice, it will tell you there's a 0% chance of success. It should tell you something like 94.53%.

    Anyone willing/able to find the problem here?
    » Horse Words «·» Roleplaying Stuff «

  • #2
    Hmm, I can't help you with the code, it turns out I remember nothing from the one or two classes I took, but when I was playing with it, I found out that it has something to do with the first digit of the number over 10.

    For example, when I put in 20 dice, it was fine with 1 or 2, and anything between 10 and 20. When I put in 30 it was fine with 1, 2 or 3, and annything between 10 and 30.

    When I put in 100 dice, it could only calculate 1, 10, and 100, so far as I determined (I did not input every number between the two, but all nothing else that I put in worked) When the number of dice was 140, 14 was added to the list, at 100% chance of success.

    That's odd.....I was poking because I could, and according to the calculator, 10 successes out of 150 dice has a 100% chance of success, but 10 successes out of 170 dice only has a 99.9999etc% chance of success.
    The High Priest is an Illusion!

    Comment


    • #3
      Quoth ArcticChicken View Post
      Hmm, I can't help you with the code, it turns out I remember nothing from the one or two classes I took, but when I was playing with it, I found out that it has something to do with the first digit of the number over 10...
      That is just odd... I imagine the problem lies in either the Combination function or the line in the Probability function that's supposed to sum up the Combinations, but I can't figure out what would cause that type of problem.

      I'd only tested it up to 15, myself.
      » Horse Words «·» Roleplaying Stuff «

      Comment


      • #4
        You actually have a number of issues in this code. I'm only going to cover the first few.

        First: Your variable names. Do not use stuff like "n" and "x" to pass around values. Use meaningful variable names, as it will make reading your code much easier for someone else. In my case, I spent about 10 minutes trying to keep straight what "n" was before I gave up, downloaded the page, and changed the variable names in my copy. "hits" and "dice" make for much easier reading when someone else has to debug the code.

        Second: You wrote your own pow() function. This is going to be slower than Math.pow(), which comes with JavaScript and is implemented (normally) in C, resulting in much faster execution. This matters when the numbers get bigger.

        Third: The actual error you requested be solved. When entering data into those fields, you are actually entering strings. JavaScript does an implicit conversion to the integer datatype for you which works well enough when the numbers are small. However, it fails when the numbers get small enough in the floating point arena (specifically in the ncr function). You need to ensure that, when you are expecting to be using numeric data, that you actually give numeric data to the functions which are expecting it. As such, you should do:

        parseFloat(document.getElementById('dice').value)

        to convert the value in the text box into the right numeric type. Once you do that, the values start producing the results you expect.

        As a final stylistic note: Your code is riddled with special cases, and has insufficient testing for valid data entry. Do everything you can to get rid of special cases, since special cases are where code breaks. Also, add in better testing of user input, so as to make sure they're entering numbers, and not 'apple', for instance.

        Comment


        • #5
          Thanks. I've fairly new to Javascript, and am actually more familiar with C++, since I did about 2 and a half years of a computer science major before switching. I didn't actually know Javascript had a power function, and got the impression it didn't after seeing similar code used on another site (but I should have realized that since it was a teaching site, they'd use examples like that).

          And the N and X variables are thanks to having three years of Probability- & Statistics-based math. I get used to using those as my variables in calculations.

          And it seems like it really doesn't need the special cases, and prob() handles it just fine. I was afraid it would give bad answers in those cases. Guess I should have trusted that bit of code more - it was the only thing I had working right from the start.
          » Horse Words «·» Roleplaying Stuff «

          Comment

          Working...
          X