Jump to content

help with a code - dont know which statement to choose


hezi_yaffe

Recommended Posts

* 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>
Edited by hezi_yaffe
Link to comment
Share on other sites

Try...

function myFunction() {  var x = document.getElementById("myInput1").value;  if (i < Ans.length){       if (x == Ans[i]) {      alert('Correct!');      i++;            if(i<Ans.length){        document.getElementById("demo").innerHTML = Que[i];      }else{        if(incorrect == 0){          alert("Perfect score! You are a good student!")        }else{          alert('You answered ' + Ans.length + ' questions with '+ incorrect + ' error(s).');        }        document.getElementById("myInput1").value = '';        document.getElementById("demo").innerHTML = 'Do you want to take the test again?';      }    } else {      alert('Sorry, wrong answer. Please try Again.');      incorrect++;    }      }else{    i = 0;    incorrect = 0;    document.getElementById("demo").innerHTML = Que[0];    document.getElementById("myInput1").value = '';  }}// end of function
Link to comment
Share on other sites

 

Try...

function myFunction() {  var x = document.getElementById("myInput1").value;  if (i < Ans.length){       if (x == Ans[i]) {      alert('Correct!');      i++;            if(i<Ans.length){        document.getElementById("demo").innerHTML = Que[i];      }else{        if(incorrect == 0){          alert("Perfect score! You are a good student!")        }else{          alert('You answered ' + Ans.length + ' questions with '+ incorrect + ' error(s).');        }        document.getElementById("myInput1").value = '';        document.getElementById("demo").innerHTML = 'Do you want to take the test again?';      }    } else {      alert('Sorry, wrong answer. Please try Again.');      incorrect++;      i = 0; // if any wrong answer go back to i=0      document.getElementById("demo").innerHTML = Que[i];    }      }else{    i = 0;    incorrect = 0;    document.getElementById("demo").innerHTML = Que[0];    document.getElementById("myInput1").value = '';  }}// end of function

 

thank you for your answer, when my answer is wrong the question dont move back to the first question but just dont change. how can i fix it?

 

davej edit: --see the revision in code above--

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...