SitePoint Sponsor 

User Tag List
Results 76 to 83 of 83

Apr 20, 2012, 16:10 #76
 Join Date
 Jan 2007
 Location
 Christchurch, New Zealand
 Posts
 14,702
 Mentioned
 101 Post(s)
 Tagged
 4 Thread(s)
Programming Group Advisor
Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
Car is to Carpet as Java is to JavaScript

Apr 22, 2012, 05:54 #77
 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.

Apr 22, 2012, 06:00 #78
 Join Date
 Jan 2007
 Location
 Christchurch, New Zealand
 Posts
 14,702
 Mentioned
 101 Post(s)
 Tagged
 4 Thread(s)
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

Apr 22, 2012, 06:17 #79
 Join Date
 Mar 2012
 Posts
 58
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Great, THANK YOU!

Apr 22, 2012, 06:23 #80
 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;
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.

Apr 22, 2012, 07:20 #81
 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?

Apr 22, 2012, 09:31 #82
 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?

Apr 22, 2012, 18:18 #83
 Join Date
 Jan 2007
 Location
 Christchurch, New Zealand
 Posts
 14,702
 Mentioned
 101 Post(s)
 Tagged
 4 Thread(s)
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
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