javascript - I'm trying to create a function that Randomizes three numbers in range (0-100) and prints the largest one -


... when call function in console returns undefined. i'm javascript newbie i'm making basic mistake, i'd greatful if me out :-).

here code:

var randomprint = function(){  x = math.floor(math.random() * 100); y = math.floor(math.random() * 100); z = math.floor(math.random() * 100);     console.log(x, y, z);     if(x > y && x > z)    {      console.log("the greatest number is" + " " + x);    }    else if(y > z && y > x)    {       console.log("the greatest number is" + " " + y);    }    else if(z > y && z > x)    {        console.log("the greatest number is" + " " + z);    } }; randomprint(); 

the answer deceze better solution, see working well. example output in console is:

35 50 47 greatest number 50 undefined 

the undefined part because function not returning anything. write as

var randomprint = function(){      x = math.floor(math.random() * 100);     y = math.floor(math.random() * 100);     z = math.floor(math.random() * 100);     console.log(x, y, z);     if(x > y && x > z) {         var biggest = x;         console.log("the greatest number is" + " " + x);    } else if(y > z && y > x) {         console.log("the greatest number is" + " " + y);        var biggest = y;    } else if(z > y && z > x) {           console.log("the greatest number is" + " " + z);        var biggest = z;    }    return biggest; };  randomprint(); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -