2015-01-13 46 views
2

當用戶點擊按鈕的日子裏,該腳本會生成一個隨機數,然後顯示相應的工作日。下面的代碼是我到目前爲止,唉,它似乎並沒有工作。 我調試過它,顯然myfunction沒有定義?!使用功能,顯示隨機數,然後ifelse語句來分配一週

<!DOCTYPE html> 
    <html> 

    <body> 

    <p>Click the button to display a random number between 1 and 7</p> 

    <button onclick="myFunction()">Get Random</button> 

    <p id="task1"></p> 

    <script> 
     function myFunction() { 
     var x = Math.floor((Math.random() * 7 + 1); 
     document.getElementById("task1").innerHTML = x; 
     } 

     if (x===1) 
     { 
      document.write("today is sunday"); 
     } 
     else if(x===2) 
     { 
      document.write("today is monday"); 
     } 
     else if(x===3) 
     { 
      document.write("today is tuesday"); 
     } 
     else if(x===4) 
     { 
      document.write("today is wednesday"); 
     } 
     else if(x===5) 
     { 
      document.write("today is thursday"); 
     } 
     else if(x===6) 
     { 
      document.write("today is friday"); 
     } 
     else if(x===7) 
     { 
      document.write("today is saturday"); 
     } 

    </script> 

    </body> 

    </html> 
+0

我注意到var x = Math.floor((Math.random()* 7 + 1);中的括號是不平衡的。 – eigenchris

回答

0

var x是在myFunction體定義當地變量。它不能在您的功能之外訪問(在您所有的if中)。

你也有你的var x = Math.floor(Math.random() * 7 + 1);聲明多餘的左括號。

修正版本:

function myFunction() { 
    var x = Math.floor(Math.random() * 7 + 1); 
    document.getElementById("task1").innerHTML = x;   

    if (x===1) 
    { 
     document.write("today is sunday"); 
    } 
    else if(x===2) 
    { 
     document.write("today is monday"); 
    } 
    else if(x===3) 
    { 
     document.write("today is tuesday"); 
    } 
    else if(x===4) 
    { 
     document.write("today is wednesday"); 
    } 
    else if(x===5) 
    { 
     document.write("today is thursday"); 
    } 
    else if(x===6) 
    { 
     document.write("today is friday"); 
    } 
    else if(x===7) 
    { 
     document.write("today is saturday"); 
    } 
} 
+0

謝謝,感謝您的解釋和修復 – Rojito

+0

@Rojito歡迎您! – zavg

0

您有一個額外的括號和代碼拋出異常的函數被渲染之前。

變種X = Math.floor(的Math.random()* 7 + 1); 的document.getElementById( 「任務1」)的innerHTML = X。;

的,你可以使用瀏覽器控制檯來查看到底是什麼地方出了錯。

1

你必須在Math.floor功能的額外括號。嘗試運行以下內容,您會看到這樣的作品。

function myFunction() { 
 
    var x = Math.floor(Math.random() * 7 + 1); 
 
    document.getElementById("task1").innerHTML = x; 
 
    
 
    if (x===1) 
 
    { 
 
     document.write("today is sunday"); 
 
    } 
 
    else if(x===2) 
 
    { 
 
     document.write("today is monday"); 
 
    } 
 
    else if(x===3) 
 
    { 
 
     document.write("today is tuesday"); 
 
    } 
 
    else if(x===4) 
 
    { 
 
     document.write("today is wednesday"); 
 
    } 
 
    else if(x===5) 
 
    { 
 
     document.write("today is thursday"); 
 
    } 
 
    else if(x===6) 
 
    { 
 
     document.write("today is friday"); 
 
    } 
 
    else if(x===7) 
 
    { 
 
     document.write("today is saturday"); 
 
    } 
 
}
<p>Click the button to display a random number between 1 and 7</p> 
 

 
    <button onclick="myFunction()">Get Random</button> 
 

 
    <p id="task1"></p>

0

,如果你使用它可以更容易和更短和對象例如

//declare the object that holds the days. 
 
    var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 
 

 
//declare your function that's triggered when clicking the button. 
 
    function myFunction(){ 
 

 
//declare your variable x that generates random numbers between 0 and 7 , 
 
// it starts from 0 because the first day "sunday" is days[0] not days[1]. 
 
    var x = Math.floor(Math.random()*7); 
 

 
//write the result in the p element 
 
    document.getElementById("task1").innerHTML ="today is "+ days[x]; 
 
    }
<button onclick="myFunction()">Click Here</button> 
 
<p id="task1"></p>

+0

用代碼回答問題時,應該包含一些關於代碼如何回答問題的文字說明。通常,僅有代碼的答案對其他人不太有用。 –