The user is supposed to enter one of several given names into the prompt box. If their response is not one of the given names, they are told that that is not a valid response. If they do, however, then they are told who they have selected. The issue is, no matter what I type in, it keeps saying that it's an invalid name. Any Advice how to fix this?
<html>
<body bgcolor = "firebrick">
<title>Dinner Plans</title>
<center><h1><b> Dinner Plans</h1>
<p> Guests to Pick From:</p></b>
<p> Jane, Lisa, Taylor, Chris, Alyssa, Will and Jessica </p>
<button onclick="Invites()">Click To Invite</button>
<div id = "Coming"></div>
<script>
function Invites() {
var txt;
var person = prompt("Who is Coming For Dinner?", "Enter Names Here");
if (person == null || person != guest) {
txt = "Not A Valid Guest.";
}
else {
txt = "Your Invited Guests Are:" + person;
}
document.getElementById("Coming").innerHTML = txt;
}
function guest ( slices , toppings ) {
this.numSlices = slices;
this.numToppings = toppings;
}
var Jane = new guest( 2, "Mushroom", "Onions", "Peppers", "Olives");
var Lisa = new guest(3, "Pepperoni", "Ham", "Pineapple");
var Taylor = new guest(3, "Extra Cheese", "Pepperoni", "Sausage", "Bacon");
var Chris = new guest(2, "Mushroom", "Sausage", "Bacon", "Ham", "Onion", "Peppers");
var Alyssa = new guest(1, " Pepperoni", "Bacon");
var Will = new guest(2, "Extra Cheese", "Sausage", "Bacon", "Onion", "Peppers", "Olives");
var Jessica = new guest(2, "Pepperoni", "Bacon", "Ham", "Pineapple", "Onion", "Peppers");
if (Invites == null || Invites == "") {
txt = " You invited zero guests, you have a pizza all to yourself!";
} else {
txt = " You Invited:" + Invites;
}
</script>
<script>
function Invites() {
var txt;
var guestArray = [
"Jane",
"Lisa",
"Taylor",
"Chris",
"Alyssa",
"Will",
"Jessica"
];
var person = prompt("Who is Coming For Dinner?", "Enter Names Here");
if (person == null || guestArray.indexOf(person) === -1) {
txt = "Not A Valid Guest.";
}
else {
txt = "Your Invited Guests Are:" + person;
}
document.getElementById("Coming").innerHTML = txt;
}
function guest ( slices , toppings ) {
this.numSlices = slices;
this.numToppings = toppings;
}
var Jane = new guest( 2, "Mushroom", "Onions", "Peppers", "Olives");
var Lisa = new guest(3, "Pepperoni", "Ham", "Pineapple");
var Taylor = new guest(3, "Extra Cheese", "Pepperoni", "Sausage", "Bacon");
var Chris = new guest(2, "Mushroom", "Sausage", "Bacon", "Ham", "Onion", "Peppers");
var Alyssa = new guest(1, " Pepperoni", "Bacon");
var Will = new guest(2, "Extra Cheese", "Sausage", "Bacon", "Onion", "Peppers", "Olives");
var Jessica = new guest(2, "Pepperoni", "Bacon", "Ham", "Pineapple", "Onion", "Peppers");
if (Invites == null || Invites == "") {
txt = " You invited zero guests, you have a pizza all to yourself!";
} else {
txt = " You Invited:" + Invites;
}
</script>
<title>Dinner Plans</title>
<center><h1><b> Dinner Plans</h1>
<p> Guests to Pick From:</p></b>
<p> Jane, Lisa, Taylor, Chris, Alyssa, Will and Jessica </p>
<button onclick="Invites()">Click To Invite</button>
<div id = "Coming"></div>
guest is constructor function. not a value(name of the guest) you are expecting.
To compare the name filled in the prompt, you need list of valid names.
For that you can use an array of names(guestArray here).
I have used indexOf function which is available on Array instances in javascript. It returns -1 when no match is found in array or index of the element if match is there.