Jump to content

hezi_yaffe

Members
  • Posts

    3
  • Joined

  • Last visited

Posts posted by hezi_yaffe

  1.  

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

  2. * 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>
×
×
  • Create New...