有“回归假”;不在函数中的“if”语句的代码?

时间:2014-01-22 18:43:50

标签: javascript

我目前正在进行课堂作业,我们正在学习if / else语句。我想知道return false;语句是否存在与if类似的代码。

例如:

var why = prompt("Why won't you fill in my prompt?");

if(why === "" || why === null){
   // How can you make this false, so that it will ask the prompt question again?
   return false; /*Like that--but not a function*/
}

2 个答案:

答案 0 :(得分:1)

首先为响应定义一个容器,然后提示它,直到它是一个可接受的值:

var reason = null;

while (reason === "" || reason === null) {
  reason = prompt("Why won't you fill in my prompt?");
}

// continue with the reason defined

答案 1 :(得分:0)

   var why=null; 

   while (why===null||why===""){
        why = prompt("Why won't you fill in my prompt?");
    }