2017-09-10 2 views
0

私はこのjQueryコードを使って単語のメモリクイズを生成しました。変数nqを除いてすべてが正常に動作しています。 最初の試みでは、コードは単純に3ワードを生成します。ユーザが正しい単語を推測した後、それは4単語を生成する別の試行のために続く。予期せず2回目の試行でカウントが2ずつ増加しましたが、コードは1だけインクリメントされました。

しかし、3回目の試行では、予期せず5単語ではなく6単語が生成されます。間違いを見つけられません。助けてもらえますか?

$(document).ready(function(){ 
// var arr is used to store temporary words generated in quiz 
var arr=[]; 
// var t is used to ask question and match result using arr array 
var t; 
// var nq is used to set number of question in each attempt. this variable not functioning right 
var nq=2; 
function arraysave(i,data){ 
    arr[i]=data; 
} 
function arrayclear(){ 
    arr=[]; 
} 
function question(data){ 
    t = data; 
} 
function nqplus(){ 
    nq++; 
} 
function nqdef(){ 
    nq=2; 
} 
alert("Please leave a like if you like this game and comment your review if any"); 
$("#startgame").click(function(){ 
$("#firstpage").slideUp(1000); 
$("#secondpage").slideDown(1000); 
}); 
$("#startgen").on('click',function(){ 
// stored words here in variable strings 
     var strings = ["apple","mango","peach","banana","orange","grapes","watermelon","tomato", "blackberry","blueberry", "chips", "bottle", "doubt", "class", "school", "country", "ocean", "foot", "hand", "hair", "mobile", "phone", "great", "sololearn", "love", "help", "sad", "bad", "good", "bottle", "neck", "laptop", "printer", "program", "relation", "ship", "true", "false", "query", "hate", "game", "guess", "name", "future", "present", "past", "city", "alone", "party"]; 
var count=-1; 
$("#startgen").hide(); 
//this function generate a word every second in quiz 
var i = setInterval(function(){ 
count++; 
var output = strings[Math.floor(Math.random() * strings.length)]; 
    arraysave(count,output); 
    $("#words").text((count+1)+"="+output); 
    if(count==nq){clearInterval(i); 
     setTimeout(function(){ $("#words").text("Now click Next");$("#next").show()}, 1000); 

    } 
},1000); 
}); 
$("#nextfinal").on('click',function(){ 
    $("#secondpage").slideUp(1000); 
    $("#thirdpage").slideDown(1000); 
    //setting here variable t to ask question e.g. what word came at place 3 
     var t = Math.floor(Math.random() * (nq+2)); 
     if(t<1){ 
      t=1; 
     } 
     if(t>(nq+1)){ 
      t=(nq+1); 
     } 
     question(t); 
     $("#question").text("Which word came at place "+t); 
     }); 
//below function works when user submit his answer after filling a text box 
     $("#submit").on('click',function(){ 
      var temp= $("#wordbox").val(); 
      if(temp==arr[(t-1)]){ 
       $("#thirdpage").slideUp(1000); 
       $("#res").slideDown(1000); 
       $(".result").text("you win"); 
       $("#nextlevel").on('click',function(){ 
       nqplus(); 
       arrayclear(); 
       $("#res").slideUp(1000); 
       $("#secondpage").slideDown(1000); 
       $("#words").text("Focus Here"); 
       $("#next").hide(); 
       $("#startgen").show(); 
       }); 
      } 
      else{ 
       $("#win").show(); 
       $(".result").text("you loose"); 
      } 
     }); 
}); 
+0

作業コードを含むフィドルを提供できますか? –

+0

もちろん、私はこの感謝を提案することを考えています。 @Dan PHilip –

答えて

1

その次の行:

$("#nextlevel").on('click',function(){ 

あなたはクリックイベントに彼らが送信ボタンをクリックするたびに適用されます。そのブロックをsubmitブロックの外側に置く必要があります。なぜなら、clickイベントを再適用する必要がないからです。

+0

はい、私はそれを固定しています。親切に、どうしてそれが起こったのか詳しく教えてください。 –

+0

@AnandBhatnagarクリックイベントスタックなので、submitを初めてクリックすると '.on( 'クリック'コードが実行されるので、'#nextlevel'をクリックすると関数が一度実行されます。あなたの '.on( 'click')を2回追加するので、次回に'#nextlevel'をクリックすると関数が2回実行されます.3回目はsubmitをクリックし、もう一度追加します – chiliNUT

関連する問題