2017-05-19 41 views
2

所以我有一些代碼,一旦它完成了一個問題,它停止了無論如何,一旦你選擇一個問題,並完成問題和正確地做它會繼續到數組中的下一個問題,但是如果你弄錯了,它會停下來。我想讓它在數組中的下一個元素在一個JavaScript測驗

我也有一個小bug,當你在一個問題數量進入,垃圾郵件Enter鍵就進入到陣列

上質疑0如果你能幫助請做的,在此先感謝。我11歲,在一年前開始編程。

<!DOCTYPE html> 
    <html> 
    <head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title>Are you smarter than a 5th Grader?!!</title> 
<link rel="stylesheet" href="style.css"> 
    </head> 
    <body onload="starter()" bgcolor="lightblue"> 
    <h1><marquee><font color="red">Make Trivia Great Again!</font>  
     </marquee></h1> 
    <h2><em><center>Are You Smater Than a 5th Grader?</center></em></h2> 
     <button onclick="sc()">Start</button><br> 
     <p id="demo"> </p> 
     <div id="result"></div> 
     <button onclick="reset()">Reset Score</button> 
     <script> 
     function starter(){ 
      setTimeout("clickCounter()",100) 
      setTimeout("minusCounter()",101)} 
     function reset(){ 
      setTimeout("clickCounter()",100) 
      localStorage.clickcount=-1 

     } 
    function clickCounter() { 
    if(typeof(Storage) !== "undefined") { 

     if (localStorage.clickcount) { 
     localStorage.clickcount = Number(localStorage.clickcount)+1; 
     } else { 
     localStorage.clickcount = 0; 
     } 
     document.getElementById("result").innerHTML = "Score:"+          
      localStorage.clickcount 
      } else { 
        } } 
     function minusCounter(){ 

     if(typeof(Storage) !== "undefined") { 

      if (localStorage.clickcount) { 
      localStorage.clickcount = Number(localStorage.clickcount)-1; 
      } else { 
      localStorage.clickcount = 0; 
      } 
      document.getElementById("result").innerHTML = "Score:"+        
      localStorage.clickcount 
     } else { 
     } 
     } 

     if (!("scramble" in Array.prototype)) { 
      Object.defineProperty(Array.prototype, "scramble", { 
      enumerable: false, 
      value: function() { 
     var o, i, ln = this.length; 
     while (ln--) { 
      i = Math.random() * (ln + 1) | 0; 
      o = this[ln]; 
      this[ln] = this[i]; 
      this[i] = o; 
     } 
     return this; 
      } 
      }); 
     } 
     var quiz = [{ 
      "question": ["Bug in the editor"], 
      "choices": ["Please Ignore,Spam Enter or enter Cancel or Other"], 
      "correct": ["Other"] 
     }, { 
        "question": "Who is the founder of Microsoft?", 
      "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak" , "Martin   
       Shaba"], 
      "correct": "Bill Gates" 
     }, { 
      "question": "What was your first dream?", 
      "choices": ["8 bits", "64 bits", "1024 bits"], 
      "correct": "8 bits" 
     }, { 
      "question": "The C programming language was developed by?", 
      "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van   
      Rossum"], 
      "correct": "Dennis Ritchie" 
     }, { 
      "question": "What does CC mean in emails?", 
      "choices": ["Carbon Copy", "Creative Commons", "other"], 
      "correct": "Carbon Copy" 
     }, { 
      "question": "What is the full for of IP", 
      "choices": ["Internet provider", "Intenet Port",    
      "Other","Internet        Protocol"], 
        "correct": "Carbon Copy" 
     }] 
     function stop(){ 
      alert("stopped") 
     } 
    function sc(){ 
     quiz.forEach(q => q.choices.scramble()); 
     var x = prompt("Select Start question number #:"); 
       if (x>=6){ 
    alert("please pick a valid question") 
       sc() 
     } 
    else if (x<=5&&x>0){ 

    } 

    else if(x=0){ 
     alert("please pick a valid question") 
       sc() 
    } 

     else{alert("Please pick a valid question"),sc()} 

     var ans = "" 
     function myFunction(item, index) { 
      ans += "\n[" + (index+1) + "]: " + item ; 
     } 
     quiz[x].choices.forEach(myFunction); 

     var y = prompt(quiz[x].question+"\nYour anwser is:"+ans); 

     if (y == quiz[x].correct){ 
      alert("Correct!") 
      clickCounter() 

     } 
      else if(y=="Cancel"){alert("canceled")} 
    else{ 
      alert("Wrong! Please Try Again."); 
      repeat() 
     } 
    function repeat(){ 
     quiz.forEach(q => q.choices.scramble()); 
     var ans = "" 
     function myFunction(item, index) { 
     ans += "\n[" + (index+1) + "]: " + item ; 
     } 
     quiz[x].choices.forEach(myFunction); 

     var y = prompt(quiz[x].question+"\nYour anwser is:"+ans); 

     if (y == quiz[x].correct){ 
     alert("Correct!,Good Job") 
     clickCounter() 


     } 
     else if(y=="Cancel"){alert("canceled")} 
     else{ 
     alert("Sorry! \nThe right answer is "+quiz[x].correct); 

     } 
    } 

    } 

     </script> 


    </body> 
    </html> 

回答

1

首先,歡迎編程的世界裏,年輕的程序員:)

之前給予解決你的問題,我想給你以下建議:

整理你的代碼。你可能認爲你需要花費大量的時間和精力去做到這一點,但事實是 - 你最終節省了大量的時間調試。

專業程序員其實大部分時間都花在計劃和調試上,而不是編碼本身。所以調試變得很簡單很重要。做到這一點的第一步是編寫整潔的代碼。

你的問題很好地證明了這一點:你不知道發生了什麼錯,你看不到它。 (這也是其他任何人閱讀和幫助下,疼痛)

它實際上是兩個簡單的問題:

  1. 你有你的功能
    一個無限循環的函數調用在其他假設情況var x,您需要添加一個返回來離開該功能。
  2. 你幾乎總是錯誤輸入x = 0而不是x == 0
    觸發這個無限循環(假設你知道x = 0是不一樣的x == 0

所以下面是所涉及的代碼:

 if (x >= 6) { 
      alert("please pick a valid question") 
      sc() 
     } else if (x <= 5 && x > 0) { 

     } else if (x == 0) { // x = 0 is assignment, not comparison 
      alert("please pick a valid question") 
      sc() 
      return; // You have to 'return' here 
        // otherwise the code following the else would continue to execute after this inner-sc() returns 
     } else { 
      alert("Please pick a valid question"), sc() 
     } 

另外,你的測驗數組中有一些換行符。 我不確定它是否只是你的拷貝&粘貼到添加了這些換行符的堆棧溢出,但如果不是,字符串不應該包含換行符。如果您需要添加換行符,請使用換行符\n(反斜槓n)

0

對不起,但我不明白你的問題。 你能告訴我更多關於它嗎?

如果您的意思是,當您嘗試在數字中輸入答案選項並且它永遠不會返回true,那麼您將number"correct" string進行比較。

您應該嘗試比較相同的數據類型以獲得所需的結果。

+0

其評論無回答 – geminiousgoel

+0

他可能沒有足夠的代表發表評論。 – kazenorin

+0

是的,我沒有足夠的聲望評論:( –

相關問題