Reputation: 9
<!DOCTYPE html>
<html lang="en">
<script type="text/javascript">
<!--
var Name = prompt("Enter Your Name");
var Age = prompt("Enter Your Age. This should be an integer.");
if (Age < 12) {
alert("Sorry, You must be at least 12 to enter this site!");
exit }
var Sex = prompt("Enter Your Sex. This should be a single letter input F or M");
if (Sex != "M", "F") {
alert("Sex must be a single letter F or M");
}
// -->
</script>
Im trying to get only an "M" of "F" from the Sex prompt, but I'm really not sure how to do that. Right now it gets to the end of the code and no matter what I type into the Sex prompt the alert "Sex must be a single letter F or M" pops up. Any help would be greatly appreciated, I've been stumped for a couple days now.
Upvotes: 0
Views: 38
Reputation: 1075427
In JavaScript and most other similar-looking languages, you have to be fairly verbose when checking against two values:
if (Sex != "M" && Sex != "F") {
Each part (Sex != "M"
and Sex != "F"
) is a complete !=
expression, and then the overall thing is an &&
("and") expression.
There are other ways to do it (a switch
, or a fairly obscure thing like if (!["M", "F"].includes(Sex))
), but the above is the standard way to do a simple check against two values.
Upvotes: 3