1

When the user enters his name in the prompt, "Hello" + name is shown in the document. That is good. But when the user enters nothing, rather than showing "Feeling shy?" it is showing "Hello Null". I don't get it why. Even my course instructor wrote the same code as mine in the video lecture in her video and it works fine but when I copy the same code it's not working properly.

var name = prompt("What is your name?");
if (name.length != null) {
  if (name == "Badar") {
    document.write("What a beautiful name");
  } else {
    document.write("Hello " + name);
  }
} else {
  document.write("Feeling shy?");
}
j08691
  • 190,436
  • 28
  • 232
  • 252

5 Answers5

0

The length of the name will never be null, it will be 0. So when you get a response with no name it checks if the length is null, which it's not, it's 0, and because it's not null it runs the code that outputs "Hello" + name.

kantuni
  • 384
  • 1
  • 7
0

When nothing is entered into the prompt, it is regarded as an empty string. You could just check if the name variable is not an empty string as so:

var name = prompt("What is your name?");
if (name != "") {
  if (name == "Badar") {
    document.write("What a beautiful name");
  } else {
    document.write("Hello " + name);
  }
} else {
  document.write("Feeling shy?");
}

You could also check for the case where a user enters in white space into the prompt:


var name = prompt("What is your name?");
if (name.trim() != "") {
  if (name == "Badar") {
    document.write("What a beautiful name");
  } else {
    document.write("Hello " + name);
  }
} else {
  document.write("Feeling shy?");
}
0

In JS to check if the variable is empty or not, you can use any of these options.

if(name !== '')

If you decide to use length then it should be like this,

if(name.length)

Please check this code.

<body>
<!--SCRIPT HERE-->
<script>
    var name = prompt("What is your name?");
    if(name.length){
        if(name == "Badar"){
            document.write("What a beautiful name");
        }
        else{
            document.write("Hello " + name);
        }
    }
    else{
        document.write("Feeling shy?");
    }
</script>
Nirav
  • 1
0

First of all, do not take name as variable, because this is a reserved property of window (Window.name).

Then have a look to the result of prompt:

  • null for cancelling the input,
  • '' for pressing enter without input,
  • 'some string' for every other input.

Now you need to check the variable and make the output.

Maybe worth a look: Why is document.write considered a "bad practice"?

var aName = prompt("What is your name?");
console.log(aName);

if (aName !== null && aName !== '') {
    if (aName === "Badar") {
        document.write("What a beautiful name");
    } else {
        document.write("Hello " + aName);
    }
} else {
    document.write("Feeling shy?");
}
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
0

Change your code like this:

var name = prompt("What is your name?");
if (name.length) {
  if (name == "Badar") {
    document.write("What a beautiful name");
  } else {
    document.write("Hello " + name);
  }
} else {
  document.write("Feeling shy?");
}

The length property always returns 0 or length of the string. So remove null checking.

Ankur Mishra
  • 1,235
  • 1
  • 4
  • 13