So I create one math game play the division operator. So there's a question where user need to answer those question. so these number was generate randomly from 1 to 10. So my question is I want those answer are return to whole number. If possible it return without remainder. How do we control the random number. I not done my research yet and this what I do.
<form action="" class="from" id="form">
<h3 class="score" id="score">Score : 0</h3>
<h1 class="question" id="question">1 + 1</h1>
<input type="text" class="input" id="input" placeholder="Enter Your Answer" autofocus autocomplete="off"/>
<button class="button">Submit</button>
<button class="reset" id="resetScore" onclick="deleteScore()">Reset</button>
</form>
<script>
const a = Math.round(Math.random() * 10);
const b = Math.round(Math.random() * 10);
// question declared
const questionDisp = document.getElementById("question");
// form declared
const form = document.getElementById("form");
// input declared
const input = document.getElementById("input");
// score declared
const score1 = document.getElementById("score");
// reset declared
const reset = document.getElementById("resetScore");
let score = JSON.parse(sessionStorage.getItem("score")); //JSON.parse to convert the string to number
if (!score) {
score = 0;
}
score1.innerText = `Score : ${score}`;
// questionDisp.innerText = `What is ${a} / ${b} = `;
// answer must not negative
if (a >= b){
const answer = a / b;
questionDisp.innerText = `What is ${a} ÷ ${b} = `;
}else if (b >= a){
answer = b / a;
questionDisp.innerText = `What is ${b} ÷ ${a} = `;
}
// answer for question declared
// const answer = a / b;
form.addEventListener("submit", () => {
const userAns = +input.value; //plus to convert string to number
if (userAns === answer) {
score++;
ScoreUpdate();
} else {
score--;
ScoreUpdate();
showAlert();
}
});
function ScoreUpdate() {
sessionStorage.setItem("score", JSON.stringify(score));
}
function showAlert() {
alert("WRONG ANSWER");
}
function deleteScore() {
sessionStorage.clear();
score = 0;
ScoreUpdate();
location.reload();
}
</script>
Your question is not clear,but i assume you want an Integer to return as an output, not a decimal numbers
If you want to check whether the result is an integer or not, you can use this
console.log(Number.isInteger(80/8)) console.log(Number.isInteger(5/3))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.