* first of all i'm sorry for the poor english.
hi everyone i new here, i need some help with a code:
i try to write a code that do that:
display a question.
if you answer it correctly, the next question will display,
if your answer is wrong you move back to the first question.
and so on...
1. i try do that with if/else statement but i think that its not very Effective.
can i use loops or something else to do that?
2. in my code, when i run it on the first time, if my answer is wrong i get back to the first question but after the first time
instead of go to the first question i just move back one question (e.g from question 3 to 2).
thanks for help!
Que and Ans.html
<!DOCTYPE HTML>
<html>
<head>
<body>
<input type="number" id="myInput1">
<button type="button" onclick="myFunction()">try it</button>
<p id ="demo"></p>
<script>
var Ans;
var Que;
var x;
var i;
Que = ["1*1", "2*2", "3*3", "4*4"];
document.getElementById("demo").innerHTML = Que[0];
function myFunction() {
x = document.getElementById("myInput1").value;
Que = ["1*1", "2*2", "3*3", "4*4"];
Ans = [1,4,9,16];
i = 0;
if (x == Ans) {
i += 1;
} else {
i = i;
}
document.getElementById("demo").innerHTML = Que;
if (x == Ans[(i+1)]) {
i += 2;
} else {
i = i;
}
document.getElementById("demo").innerHTML = Que;
if (x == Ans[(i+2)]) {
i += 3;
} else {
i = i;
}
document.getElementById("demo").innerHTML = Que;
if (x == Ans[(i+3)]) {
window.alert("you are a good student!")
} else {
document.getElementById("demo").innerHTML = Que[(i)];
}
}
</script>
</body>
</head>
</html>