簡體   English   中英

嘗試生成特定范圍內的隨機整數,然后內聯顯示

[英]Trying to generate a random integer within a specific range then have it displayed inline

所以,這就是我所擁有的(我正在運行JQuery):

http://jsfiddle.net/KDmwn/111/

The computer chose <span id="x"></span>. 
$(document).ready(function () {
    var x = function getRandomInt(1, 4) {
        return Math.floor(Math.random() * (4 - 1 + 1)) + 1
    };
    $('#x').html(x);
}

我覺得問題與這個$('#x').html(x);

任何幫助深表感謝。 謝謝!

問題是因為您的x函數將兩個整數設置為參數,從語法上講這是不正確的。

為了實現所需的功能,您應該刪除參數列表中的整數,修復DOMReady處理程序末尾的不匹配括號,還可以從Math.random值中刪除- 1 + 1

嘗試這個:

var x = function getRandomInt() {
    return Math.floor(Math.random() * 4) + 1;
}
$('#x').html(x);

更新的小提琴

使用以下代碼

$('#x').html(Math.floor(Math.random() * 4 ) + 1);

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM