Unable to get IF..ELSE condition to work using the prompt

Hi There,

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.

Thanks so much!

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 :lol: )…

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!");
}
1 Like

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.

Can you kindly help with that?

Much appreciated! :slight_smile:

Again, prompt doesn’t return null (I know, the documentation says it does - testing tells differently).

To resolve having an empty result, check the length of name and if greater than zero, then print the name, otherwise fall into the else.

so you’ll be changing this line. Either add another condition, or replace the first one (since we proved that null is not what prompt returns)

if (name !== null && name !== "null"){

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.

so instead of

name !== null

try

name.length > 0

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)

2 Likes

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');
}
1 Like

PERFECT!! You are AWESOME!! :joy:

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!");
  }

Have a Terrific Day!

Hi Benanamen,

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.

null is returned when the user clicks on the cancel button :slight_smile:

const response = prompt('Click Cancel'); // click cancel

console.log(response === null); // true

@Loranne
You don’t need the name !== 'null'. That’s testing for a string of 'null' not the value null.
See MDN - null

const response = prompt('Type null'); // type null and hit enter

console.log(response === 'null'); // true

or

console.log(response === null); // false

As benanamen pointed out, the following will suffice.

if (response){
  alert ('All done, '+response+'. Your results are ready.');
}

All responses, with the exclusion of cancel, are strings.

I recommend reading the following links

1 Like

You’re Right!! I love this team! This WORKS TOO! :joy:

Much appreciated!

Huh…It didn’t for me (using Chrome). It returned the string “null”. Don’t know why.

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 .

Don’t know mate :slight_smile:

Wow! This is a wealth of information. I’m learning alot on this site.
The MDN links are very helpful.

Thanks :slight_smile:

1 Like

Perhaps you did this which will return null instead of true.

const response = prompt('Click Cancel'); // click cancel
console.log(response);// null

I was debugging the code from the OP and was cancelling every time.

I added an alert of name, which returned null. But the if statement didn’t work.

So I added an alert of name.length, which returned 4.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.