尝试生成特定范围内的随机整数,然后将其显示为内联

时间:2015-05-13 11:38:44

标签: javascript jquery html

所以,这就是我所拥有的(我正在运行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);

有关

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:4)

问题是因为你的x函数有两个整数设置为参数,这在语法上是不正确的。

要实现所需,您应该删除参数列表中的整数,修复DOMReady处理程序末尾的不匹配括号,还可以从- 1 + 1值中删除Math.random

试试这个:

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

Updated fiddle

答案 1 :(得分:0)

使用以下代码

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

Fiddle