I'm a new to Javascript and I'm trying out some basic scripts. My objective is to display a prompt message (1.) asking for a name, then show an alert (2.) based on the input from the prompt. If the input is a valid name (a string), then the alert should say "thank you" followed by the name. However, if the input is not a valid name, such as a number, the alert should say "you didn't enter a valid name."
The first prompt message is functioning correctly, but the second alert message displays the same text regardless of whether I enter a word or a number. This means that the alert isn't able to recognize the input type and simply shows the same message of "thank you" + the entered value.
Here is the code I'm using:
function EnterName() {
var name = prompt("Please enter your name:");
if (typeof name === "string") {
alert("Thank you " + name);
} else if (typeof name === "number") {
alert("You didn't enter a valid name");
}
}
console.log(EnterName());
I would greatly appreciate any advice on how to ensure that the alert box displays "you didn't enter a valid name" when a number is entered in the prompt field.
Thank you!