2016-12-27 176 views
0

尋找來自那些比我更有見識的人的一些指導。隨機數字生成器Javascript - 範圍

我正在寫一個程序,提示用戶點擊一個按鈕,然後將生成到100

1的隨機整數我可以管理這部分罰款。我正在努力的是讓程序打印這個隨機整數的範圍,即我< = 33。

例如,當我點擊按鈕我想見:

「10比33更小的號碼」

「55是33和66之間的數字」

這是我的代碼到目前爲止,我已經把代碼,我在評論中有問題。

<!DOCTYPE HTML5> 

<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>question2</title> 
</head> 

<body> 

    <p>Click the button to display a random number from 1 to 100.</p> 

    <button onclick="myFunction()">Click here</button> 

    <p id="demo"></p> 

    <script> 
    function myFunction() { 
     var text = ""; 
     var i = Math.floor((Math.random() * 100) + 1); 
     document.getElementById("demo").innerHTML = i; 

     } 

//   if (myFunction <= 33) { 
//   text += ("<br>" + i + " is a number less than or equal to 33"); 
//   } 

//   if (myFunction > 34 and i < 65) { 
//   text += ("<br>" + i + " is a number between 33 and 66 (exclusive)"); 
//   } 

//   if (my Function >= 66) { 
//   text += ("<br>" + i + " is a number greater than or equal to 66"); 
//   } 



    </script> 

</body> 
</html> 

謝謝你們。

+1

其實,你與函數的概念掙扎。我可以建議退後一步,用普通的JavaScript播放一下沒有混淆瀏覽器/ DOM的考慮?您的shell中的nodejs將會是一個不錯的遊樂場。 –

+0

@justsomebody NodeJS聽起來不像「普通JavaScript」。 [瀏覽器控制檯](http://webmasters.stackexchange.com/q/8525)已經足夠了。 – Xufox

+0

@justsomebody對此我完全陌生,感謝您的建議,我想我一次試圖做得太多。需要更多的研究工作! – Keeron

回答

1

您是不是要找這樣的事:

function myFunction() { 
     var text = ""; 
     var i = Math.floor((Math.random() * 100) + 1); 

     if (i <= 33) { 
      text += ("<br>" + i + " is a number less than or equal to 33"); 
     } 

     if (i > 34 && i < 65) { 
      text += ("<br>" + i + " is a number between 33 and 66 (exclusive)"); 
     } 

     if (i >= 66) { 
      text += ("<br>" + i + " is a number greater than or equal to 66"); 
     } 

     document.getElementById("demo").innerHTML = text; 
    } 
+0

@Xufox好趕上,我copypasted和名義上重新格式化的代碼沒有測試:) – khalid13

+0

這正是我的意思:)我最初嘗試過這種方法,無法看到我看到你的代碼,但至少犯了什麼錯誤,但至少我知道我在正確的軌道上! – Keeron

0

不知道爲什麼,但我不得不這樣做,因爲函數表達式,而不是一個函數的定義,使其工作。奇怪的是,這是第二次在最後一個小時裏作爲一個陷阱工作。

myFunction = function() { 
 
    var text = ""; 
 
    var i = Math.floor((Math.random() * 100) + 1); 
 
    document.getElementById("demo").innerHTML = i; 
 
    switch (true) { 
 
    case (i < 20): 
 
     console.log(i + " is less than 20."); 
 
     break; 
 
    case (i < 40): 
 
     console.log(i + " is between 21 and 40."); 
 
     break; 
 
    case (i < 60): 
 
     console.log(i+ " is between 21 and 60."); 
 
     break; 
 
    default: 
 
     console.log(i+ " is over 60."); 
 
    } 
 
    };
<p>Click the button to display a random number from 1 to 100.</p> 
 

 
    <button onclick="myFunction()">Click here</button> 
 

 
    <p id="demo"></p>

+0

您不必將其作爲函數表達式。此外,這不能回答這個問題。 – Xufox

+0

謝謝你的提醒,我已編輯帖子以顯示可能的解決方案。 – Snowmonkey

+0

謝謝Snowmonkey,很高興能夠獲得另一個角度,我一定會用switch語句去使用這個解決方案來指導我的嘗試:) – Keeron

2

您可以測試,如果true顯示結果並退出。

function getRandom() { 
 
    var i = Math.floor((Math.random() * 100) + 1); 
 

 
    if (i <= 33) { 
 
     document.getElementById("demo").innerHTML += i + " is a number less than or equal to 33<br>"; 
 
     return; 
 
    } 
 
    if (i < 66) { 
 
     document.getElementById("demo").innerHTML += i + " is a number between 33 and 66 (exclusive)<br>"; 
 
     return; 
 
    } 
 
    document.getElementById("demo").innerHTML += i + " is a number greater than or equal to 66<br>"; 
 
}
<p>Click the button to display a random number from 1 to 100.</p> 
 
<button onclick="getRandom()">Click here</button> 
 
<p id="demo"></p>

+0

尼斯,感謝您的輸入:) – Keeron

1

的問題是你調用從內myFunction()之外的功能的變量。例子包括imyFunction()。將if else語句放在主函數中會更好。

function myFunction() { 
 
    var demo = document.getElementById("demo"); 
 
    var i = Math.floor((Math.random() * 100) + 1); 
 
    if (i <= 33) { 
 
     demo.innerHTML = i + " is a number less than 33"; 
 
    } 
 
    else if (i < 65) { 
 
     demo.innerHTML = i + "is a number between 33 and 65"; 
 
    } 
 
    else { 
 
     demo.innerHTML = i + " is a number greater than or equal to 66"; 
 
    } 
 
}
<p>Click the button to display a random number from 1 to 100.</p> 
 

 
    <button onclick="myFunction()">Click here</button> 
 

 
    <p id="demo"></p>

+0

感謝理查德,這使得更有意義,我最初在看別的語句,但我認爲,看着你的建議,我把它們稱爲功能之外。我會得到它最終的hang :) :) – Keeron

1

您的功能沒有什麼,你應該對你的if語句比較。

但是,我確實把東西扔在一起讓我知道,如果這是你在找什麼。

你在正確的軌道上。在中間比較中,我改變了你「和」到一個短路「& &」。

的jsfiddle:https://jsfiddle.net/xew8dvbd/

$(document).ready(function() { 
function myFunction() { 
    var i = Math.floor((Math.random() * 100) + 1); 
    var text = ""; 
    if (i <= 33) { 
    text += ("<br>" + i + " is a number less than or equal to 33"); 
    } 
    if (i > 34 && i < 65) { 
    text += ("<br>" + i + " is a number between 33 and 66 (exclusive)"); 
    } 
    if (i >= 66) { 
    text += ("<br>" + i + " is a number greater than or equal to 66"); 
    } 
    document.getElementById("demo").innerHTML = text; 
}; 
$("#submit").click(myFunction); 
}); 
+0

感謝西西,你的幫助是非常感謝。 – Keeron