# Thread: how can I make a simple mind game, that includes addition, multiplications...

1. Originally Posted by faez
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.

2. 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. Originally Posted by faez
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.

4. Great, THANK YOU!

5. 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
}```

6. 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.

7. I have also tried
case 4:
question.operator = 'percent of';
break;

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

8. Originally Posted by faez
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
}```

#### Posting Permissions

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