Javascript 使用条件if退出程序
我有一个疑问,我想退出有条件的程序,例如:Javascript 使用条件if退出程序,javascript,if-statement,Javascript,If Statement,我有一个疑问,我想退出有条件的程序,例如: var number = prompt("Choice a number between 1 and 6"); var random = Math.floor(Math.random() * 6) + 1; if (parseInt(number) > 6){ document.write("<p>Please, choice a number between 1 and 6</p>
var number = prompt("Choice a number between 1 and 6");
var random = Math.floor(Math.random() * 6) + 1;
if (parseInt(number) > 6){
document.write("<p>Please, choice a number between 1 and 6</p>")
// Here, I want to exit of conditional and program.
} else {
if()
}
if() {
} else {}
这是可能的
非常感谢程序执行的主线程属于浏览器。JavaScript是按需调用的;JavaScript代码不控制浏览器。因此JavaScript代码无法退出控件的主线程或浏览器。
在您的情况下,您可以简单地使用return关键字。请记住,JS是一种函数式语言,因此当函数结束时,脚本将结束。也许您会更改比较的逻辑
if (parseInt(number) <= 6) {
// do other stuff
} else {
document.write("<p>Please, choice a number between 1 and 6</p>")
// Here, I want to exit of conditional and program.
}
// this is the end of the program
你的代码似乎与你的问题不匹配。如果条件if为true,但if为false,我不希望继续代码继续…的可能重复