Can someone PLEASE assist me with a Java IF/Else problem?
I’m currently enrolled in a Javascript class, Yay!
I was given an assignment to insert a name via a prompt and if a name is not inserted or the person press the Cancel button, the second condition is executed.
For some reason, the second condition using ELSE is not working.
Here are the my class instructions.
All done, STUDENT. Your results are ready."
i. Replace “STUDENT” with the name captured by the user in the prompt window.
b. If they did not complete their name, the message should read:
“All done, visitor. Your results are ready.” (SECOND Condition)
My first statement runs with any name the user enters.
The second condition runs but removes the “visitor” name out of the sentence.
My Code:
var name = prompt("What is your name?");
if (name !=null){
alert ("All done, "+name+". Your results are ready.");
} else {
prompt("All done, visitor. Your results are ready.");
}
Note: I must use a If function that uses correct logical operator to compare the user’s input to an empty value.
I’ve never used prompt before, but in this case, it looks like null isn’t actually null but “null”
This seems to work (note - you had a prompt in the else instead of an alert, I fixed that for ya )…
var name = prompt("What is your name?");
if (name !== null && name !== "null"){
alert ("All done, "+name+". Your results are ready.");
} else {
alert("All done, visitor. Your results are ready!");
}
YES!! IT WORKED!! Thank you for your fast response.
The second condition works if you press Cancel.
If you leave the prompt blank and click on OK… We still have the same problem…“visitor” doesn’t display.
Thanks Dave. It still doesn’t work with clicking OK on an empty box.
Let’s change the first condition.
What do you suggest besides using null. I’m not sure.
To be honest, most people may just click cancel to not enter anything.
I was just trying to cover both scenarios - clicking Cancel or OK on an empty box.
Though thinking on it, it probably doesn’t hurt to have the null check in (chromium based browsers return a string, FF and others may not). So add the length check as an additional condition (another set of && and the length check above)
First off don’t use use name with var. This is all you need.
var response = prompt("What is your name?");
if (response){
alert ('All done, '+response+'. Your results are ready.');
} else {
alert ('Canceled or blank');
}
When I clicked on cancel and when I click on OK without entering anything, the second condition executed perfectly!
var name = prompt("What is your name?");
if (name.length > 0 && name !== "null") {
alert("All done, " + name + ". Your results are ready.");
} else {
alert("All done, visitor. Your results are ready!");
}
Thanks for your help… My assignment is to the below… Instead of var name… should I use let name?
Assignment:
declare a variable “name” to equal the prompt “Please enter your name”. This way, the variable will be set to be the value of the user’s answer in the prompt window, which will be stored as a string.
That’s odd. I had to check again. If I paste that code into chrome’s console, then when prompted click on the cancel button I get true.
MDN
When the user clicks the OK button, text entered in the input field is returned. If the user clicks OK without entering any text, an empty string is returned. If the user clicks the Cancel button, this function returns null .