开发者

Unexplained error in JS

开发者 https://www.devze.com 2023-02-23 17:29 出处:网络
This is the code: var k ; function getrandom() { k = Math.random()*2 ; alert(k); } Hello I\'ve searched a lot to see why this simple JS code is开发者_如何学运维 not working. I appreciate your help.

This is the code:

var k ;
function getrandom()
{
    k = Math.random()*2 ;
    alert(k);
}

Hello I've searched a lot to see why this simple JS code is开发者_如何学运维 not working. I appreciate your help. Thank You.


Maybe you forgot to call it.

var k;   

function getrandom()
{    
    k = Math.random() * 2;
    alert(k);
}

getrandom();


If I execute your snippet and invoke getrandom() it alerts a random number between 0 and 2. Is that not what the program is supposed to do?


Maybe you don't call this function?

Try this fiddle: http://jsfiddle.net/MRcmF/

It works!


I think you meant to do something like this:

function getrandom(max) {
    alert(Math.round(Math.random()*max)) ;
}
getrandom(10);
0

精彩评论

暂无评论...
验证码 换一张
取 消