Unexplained error in JS
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);
精彩评论