SitePoint Sponsor

User Tag List

Page 4 of 4 FirstFirst 1234
Results 76 to 83 of 83
  1. #76
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by faez View Post
    Could you kindly, indicate where I need to change?
    It seems that nothing there needs to be changed.

    Math.floor(Math.random() * 5) results in a random value that ranges from 0 to 4.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  2. #77
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you!!!

    Could you also advice what is the mathematical equation for power? is there a special character for calculating power of an integer in JavaScript?

    I am sorry for asking many questions, I have decided to build this game for my young brother, and I am excited to make this game fun, educational and relaxing.

  3. #78
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by faez View Post
    Could you also advice what is the mathematical equation for power? is there a special character for calculating power of an integer in JavaScript?
    It is the Math.pow(base, exponent) method.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #79
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, THANK YOU!

  5. #80
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am still not able to make the var numberOfQuestions to work - as at run time it does not change the question number[/quote]

    Okay, so let's simplify that numberOfQuestions part to help you understand what is happening there.

    Code:
    var numberOfQuestions = parseInt(window.prompt('...')), 10) || 10;
    Breaking it apart in to its constituent pieces, we have:

    Code javascript:
    var response = window.prompt('...'); // ask for the number of questions
    var numberOfQuestions = parseInt(response, 10); // convert to a decimal integer
    if (!numberOfQuestions) { // if it cannot be turned in to a decimal integer
        numberOfQuestions = 10; // use a default value instead
    }
    Last edited by paul_wilkins; Apr 22, 2012 at 18:09.

  6. #81
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a quick question,

    my equation percent is working but gives the wrong answers. for example, 3 percent of 10 is 30 and not 0.3


    I have tried changing the equation to

    case 4:
    question.operator = 'percent of';
    question.answer = ((question.first /100 ) * question.second);
    break;

    also tried

    case 4:
    question.operator = 'percent of';
    question.answer = ((question.first / question.second) *10000);
    break;

    but with this equation, the browser does not run the code when randomly reaches the percent code, and alerts to stop the script, at run time.
    could you advice a better way to code this please?

  7. #82
    SitePoint Enthusiast
    Join Date
    Mar 2012
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have also tried
    case 4:
    question.operator = 'percent of';
    question.answer = question.first % question.second;
    break;

    but still is not working - the given answer is incorrect?

  8. #83
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by faez View Post
    just a quick question,

    my equation percent is working but gives the wrong answers. for example, 3 percent of 10 is 30 and not 0.3
    Are you trying to say that 3 percent of 10 should be an answer of 30? Because that's not correct. The correct answer is 0.3

    The maths that you had with ((question.first / 100) * question.second) does seem to result in the right answer.

    Code:
    > question = {
          first: 3,
          second: 10
      };
    > (question.first / 100) * question.second
      0.3
    With JavaScript though, decimal fractions can be slightly off due to computers using double-precision numbers, which results in 0.1 + 0.2 equalling something close to but not actually 0.3

    One way to deal with that is to check if the answer is "close enough" to be correct, by checking if it falls within a certain allowed precision. For example:

    Code javascript:
    if (Math.abs(Number(question.guess) - answer) > 0.001) {
        ... // is correct
    } else {
        ... // is wrong, since guess is off by more than 1/1000
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •