patternjavascriptMinor
Maths Q&A game in JavaScript
Viewed 0 times
mathsgamejavascript
Problem
This used to be university work now I put it our here for review purposes.
`
Math.floor
Math.floor: The Maths Game
// Universty work
// @author Darik
// Math.floor
var score = 0;
var times = 0;
function questions(e) {
var x = Math.floor(Math.random() * 11);
var y = Math.floor(Math.random() * 11);
var z = 0;
var sign = '';
if (e == 1) {
sign = '+'
z = x + y;
interact(x, sign, y, z)
} else if (e == 2) {
sign = '-'
z = x - y;
interact(x, sign, y, z)
} else if (e == 3) {
sign = '/'
z = x / y;
interact(x, sign, y, z)
} else {
sign = '*'
z = x * y;
interact(x, sign, y, z)
}
}
function interact(x, sign, y, z) {
times++;
var userAnswer = prompt("What is " + x + " " + sign + ' ' + y + "?");
if (z == userAnswer) {
document.write("
" + x + " " + sign + ' ' + y + " = " + userAnswer + " is correct ");
score = score + 1;
} else {
document.write("
" + x + " " + sign + ' ' + y + " = " + userAnswer + " is INCORRECT. The correct answer is " + z + "");
}
}
function _score() {
if (times == score) {
document.write('
A perfect score ');
document.body.style.backgroundColor = "#FF99CC";
} else {
document.body.style.backgroundColor = "#CCFFFF";
}
document.write('
You have answerd ' + score + ' questions out
- Could this be constructed better?
- Is there any easier path, performance-wise?
- Could this be optimised?
- Which features and functionalities could/should be added to this game?
`
Math.floor
Math.floor: The Maths Game
// Universty work
// @author Darik
// Math.floor
var score = 0;
var times = 0;
function questions(e) {
var x = Math.floor(Math.random() * 11);
var y = Math.floor(Math.random() * 11);
var z = 0;
var sign = '';
if (e == 1) {
sign = '+'
z = x + y;
interact(x, sign, y, z)
} else if (e == 2) {
sign = '-'
z = x - y;
interact(x, sign, y, z)
} else if (e == 3) {
sign = '/'
z = x / y;
interact(x, sign, y, z)
} else {
sign = '*'
z = x * y;
interact(x, sign, y, z)
}
}
function interact(x, sign, y, z) {
times++;
var userAnswer = prompt("What is " + x + " " + sign + ' ' + y + "?");
if (z == userAnswer) {
document.write("
" + x + " " + sign + ' ' + y + " = " + userAnswer + " is correct ");
score = score + 1;
} else {
document.write("
" + x + " " + sign + ' ' + y + " = " + userAnswer + " is INCORRECT. The correct answer is " + z + "");
}
}
function _score() {
if (times == score) {
document.write('
A perfect score ');
document.body.style.backgroundColor = "#FF99CC";
} else {
document.body.style.backgroundColor = "#CCFFFF";
}
document.write('
You have answerd ' + score + ' questions out
Solution
You ask some unreasonable division questions.
That is answerable by the special string
The expected answer is
10 / 3 = 3.33333333 is INCORRECT. The correct answer is 3.3333333333333335
That is answerable by the special string
Infinity, but not infinity.The expected answer is
NaN, but responding with NaN will not work.10 / 3 = 3.33333333 is INCORRECT. The correct answer is 3.3333333333333335
Context
StackExchange Code Review Q#84413, answer score: 6
Revisions (0)
No revisions yet.