使用条件if退出程序

Exit from program with conditional if

本文关键字:退出程序 if 条件      更新时间:2023-09-26

我有一个疑问,我想退出程序与条件的如果,例如:

    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 {}

´s。

非常感谢

也许你改变了比较的逻辑

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

执行程序的主线程属于浏览器。JavaScript是按需调用的;JavaScript代码不控制浏览器。因此JavaScript代码不能退出控制的主线程或浏览器。在您的情况下,您可以简单地使用return关键字。请记住,JS是一种函数式语言,所以当函数结束时,脚本也会结束。