SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple division question

    I don't think I understand how js handles division, or the logic i'm using here is really wrong....

    var amountwrong = (6 - amountcorrect);

    var percent = amountwrong / amountcorrect;

    alert(percent);

    The variable amountcorrect can be anything from 0 - 6. My alert is telling me that 2 / 6 = .5, instead of .33333333 as expected or that 3 / 6 = 1, not 0.5?

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your math is wrong, not the way JavaScript is handling it. To get the percentage of correct answers out of a total of 6:

    Code:
    var percent = (amountcorrect * 100) / 6;

  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •