[英]True and False return of functions in Javascript
I would like to display the message “Yes” or “No” for getting a function return of true or false respectively (not directly changing the word true and false) Display but not using alert.我想显示消息“是”或“否”以分别获得真或假的函数返回(不直接改变单词真假)显示但不使用警报。
How can I do that?我怎样才能做到这一点? Thanks.
谢谢。
function myFunction {
if (Math.floor(Math.random()*2) == 0) return false
else return true;
}
if (myFunction()) { console.log("Yes"); }
else { console.log("No"); }
I see you are new, so welcome :v --我看到你是新来的,所以欢迎 :v --
Way 1: document.write(myFunction()? 'yes' : 'no');
方式 1:
document.write(myFunction()? 'yes' : 'no');
Way2:方式2:
<p id="demo" ></p>
<button onclick="myFunction()"> yes or no </button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = myFunction()? 'yes' : 'no';
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.