Reputation: 134
I have tried adding a Do/While loop, then I tried a If statement but I can't seem to get this to work.
I am trying to determine if the user hasn't made three guesses, and hits cancel on the userGuess prompt .. it would then return a alert("You are a chicken");
//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 />");
}
Upvotes: 0
Views: 843
Reputation: 134
I ended up going with the following and it does the job, in this particular case.
// if user hits cancel on the sGuess prompt
if (!sGuess && loops < 4) {
alert("You are a chicken");
}
Upvotes: 0
Reputation: 22061
Update sGuess
checking from:
if (sGuess == "") {
alert("You are a chicken");
}
to next one:
if (sGuess == null) {
alert("You are a chicken");
}
If user click cancel sGuess would be equal to null
, to verify third user tries was ended with cancel pressing add checking for loops
counter value (sGuess == null && loops == 3)
.
Upvotes: 1
Reputation: 63
"prompt" is returning null if the user hits cancel. In this case all your substr and subsequent code will fail.
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");
}
...
Upvotes: 0
Reputation: 65853
Just check the result to see if there was a value supplied:
name = prompt("Enter your first name", "");
// If no name value was received:
if(!name){
alert("Chicken!");
}
Upvotes: 0
Reputation: 388436
The prompt return s a null
value where clicked on cancel, so your substring()
method fails with an error(Uncaught TypeError: Cannot read property 'substr' of null
).
You need to check it as soon as the prompt is called, then continue like
var sGuess = prompt("Enter a sport guess", "");
if (sGuess == "") {
alert("You are a chicken");
continue;
}
Upvotes: 2