javascript - counting unexpectedly increased by 2 in 2nd attempt, while i coded it to just increment by 1? -
i wrote jquery code generate words memory quiz. working fine except variable nq
. in first attempt, code generates 3 words. after user has guessed correct word, goes on trial in generates 4 words.
however, in third trial unexpectedly generates 6 words instead of 5. unable find mistake. can help?
$(document).ready(function (){ // var arr used store temporary words generated in quiz var arr=[]; // var t used ask question , match result using arr array var t; // var nq used set number of question in each attempt. 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 if game , comment 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 word every second in quiz var = 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 ask question e.g. word came @ 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 @ place "+t); }); //below function works when user submit answer after filling 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"); } }); });
its line:
$("#nextlevel").on('click',function(){
you applying click event every time click submit button. need put block outside of submit block, since don't want keep reapplying click event.
Comments
Post a Comment