如何使警报打印超过50,低于50(如果声明)?

时间:2017-08-14 05:38:53

标签: javascript

function randomise(){
    var ran_number=Math.floor(Math.random() * 100);
    return ran_number;
    return(ran_number);
}

我想if语句如下所示。

alert(randomise + "You scored over 50, You win");
alert(randomise + "You scored under 50, You lose");

我该怎么做?

3 个答案:

答案 0 :(得分:3)

var value = randomise();
if (value > 50)
  alert(value + " You scored over 50, You win");
else
  alert(value + " You scored under 50, You lose");

答案 1 :(得分:0)

试试这个:

var ran_number = Math.floor(Math.random() * 100);

if(ran_number > 50){
  alert(ran_number+"  You scored over 50, You win");
}else{
  alert(ran_number+"  You scored under 50, You lose");
}

答案 2 :(得分:0)

继续Geoffrey's answer - 而不声明变量:

function randomise(){
    return Math.floor(Math.random() * 100);
}

if( (i = randomise()) > 50 ) {
    alert('You scored '+i+', you win!'); 
} else {
    alert('You scored '+i+', you lose...'); 
}

我要留下这个让孩子们学习Javascript的魔力:

if( (i = Math.floor(Math.random() * 100)) > 50 ) { alert('You scored '+i+', you win!'); } 
else { alert('You scored '+i+', you lose...'); }

用一个按钮告诉你你的表现如何:

document.getElementById('buttown').onclick = function(){
  if( (i = Math.floor(Math.random() * 100)) > 50 ) { alert('You scored '+i+', you win!'); } 
  else { alert('You scored '+i+', you lose...'); }
};
<button id="buttown">Click here</button>