Karimko
Karimko

Reputation: 1

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.

Upvotes: 0

Views: 73

Answers (4)

Gazler
Gazler

Reputation: 84150

I think you meant to do something like this:

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

Upvotes: 1

Eugene
Eugene

Reputation: 11270

Maybe you don't call this function?

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

It works!

Upvotes: 1

BoltClock
BoltClock

Reputation: 723688

Maybe you forgot to call it.

var k;   

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

getrandom();

Upvotes: 5

Jim Blackler
Jim Blackler

Reputation: 23169

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?

Upvotes: 1

Related Questions