如果用户"取消"提示框然后

时间:2016-03-14 06:49:22

标签: javascript

我试过添加一个Do / While循环,然后我尝试了一个If语句,但我似乎无法让它工作。

我正在尝试确定用户是否进行了三次猜测,并在userGuess提示符下点击取消..然后它会返回一个警报(&#34;你是一只鸡&#34;); < / p>

//declare variables
var sportsArray = new Array("Football", "Basketball", "Rollerblading", "Hiking", "Biking", "Swimming");
var name = "";
var score = 0;
var loops = 0;


// prompts for user name, checks for input.
do {
    name = prompt("Enter your first name", "");
}
while (name == "");


for (loops = 1;loops <=3; loops++) {

    var sGuess = prompt("Enter a sport guess", "");
    // uses substrings to ultimately capitalize the 1st letter, and make everything after it lowerCase.
    var sFirstPart = sGuess.substr(0, 1);
    var sFirstCap = sFirstPart.toUpperCase();
    var sSecondPart = sGuess.substring(1, sGuess.length);
    var sSecondLow = sSecondPart.toLowerCase();
    // concats the two parts into one string
    var usableGuess = sFirstCap + sSecondLow;

    // if user hits cancel on the sGuess prompt
    if (sGuess == "") { 
        alert("You are a chicken");
    }

    // checks if usableGuess is contained in the arry or not.
    if (sportsArray.indexOf(usableGuess) === -1) { 
            document.write("Sorry! Try again.<br />");
            score = score -5;
        } 

        else { 
            document.write("You are good, try again.<br />"); 
            score = score + 5;
            }
    }
    //depending on the user score, prompts one of three messages.
    if (score < 0) { 
        document.write(name + ", you do not demonstrate ESP tendencies at this time.<br />");
        } else if (score < 15) { 
            document.write(name + ", you are not bad.<br />");
            } else { 
                document.write("<br/>" + name + ", you are a mind reader!<br />");
            }

5 个答案:

答案 0 :(得分:1)

提示返回null值,点击取消,因此substring()方法失败并显示错误(Uncaught TypeError: Cannot read property 'substr' of null)。

您需要在调用提示后立即检查,然后继续

  var sGuess = prompt("Enter a sport guess", "");
  if (sGuess == "") {
    alert("You are a chicken");
    continue;
  }

答案 1 :(得分:1)

从以下地址更新sGuess

if (sGuess == "") { 
        alert("You are a chicken");
}

到下一个:

if (sGuess == null) { 
        alert("You are a chicken");
}

如果用户点击取消sGuess等于null,则验证第三次用户尝试是否已取消,请按取消添加检查loops计数器值(sGuess == null && loops == 3)

答案 2 :(得分:0)

只需检查结果,看看是否提供了值:

 name = prompt("Enter your first name", "");

 // If no name value was received:
 if(!name){
      alert("Chicken!");
 }

答案 3 :(得分:0)

如果用户点击取消,

“prompt”返回null。在这种情况下,所有substr和后续代码都将失败。

    var sGuess = prompt("Enter a sport guess", "");
    if(sGuess !== null) {
        // uses substrings to ultimately capitalize the 1st letter, and make everything after it lowerCase.
        var sFirstPart = sGuess.substr(0, 1);
        var sFirstCap = sFirstPart.toUpperCase();
        var sSecondPart = sGuess.substring(1, sGuess.length);
        var sSecondLow = sSecondPart.toLowerCase();
        // concats the two parts into one string
        var usableGuess = sFirstCap + sSecondLow;

    } else {
        // if user hits cancel on the sGuess prompt
        alert("You are a chicken");
    }

...

答案 4 :(得分:0)

在这个特殊情况下,我最终选择了以下内容并完成了工作。

    // if user hits cancel on the sGuess prompt
    if (!sGuess && loops < 4) { 
        alert("You are a chicken");
    }