Ryan Ryan - 3 years ago 205
jQuery Question

Jquery if output is over 60 output "you won!" else "too bad you lost"

So I've got this script that outputs a random number between 0 and 100 every time you click on the button

$(document).ready(function(){
for ( i = 0; i < 1; i++ ) {
$('#btn').on('click', function(){
var randNum = 0;
var randNum = Math.floor(Math.random() * 100);

$(".result").html(" "+randNum);
});
};

});


Now do I want to have the outcome to have something like "you won" when a score of 60 or higher has been obtained otherwise "you lose". I appreciate any help!

Answer Source

You can do this:

$(document).ready(function(){
    $('#btn').on('click', function(){
        var randNum = 0;
        var randNum = Math.floor(Math.random() * 100);
        var winlose = randNum >= 60?'You win':'You lose';

        $(".result").html(winlose + ' ' +randNum);
    });
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download