You're using the wrong equals here:
if (name="John" && "Penny" && "Pat")
Change it to
if (name === "John" || name === "Penny" || name === "Pat")
You're also setting name
incorrectly:
var name = ("John" & "Penny" & "Pat")
should be
var name = ["John", "Penny", "Pat"];
(if you want an array.) (Though you're overwriting it in the next line, so why ever set it like that in the first place?).
Finally, it's bad practice to use document.write
, try console.log
instead.
To sum up so far:
var name = prompt("What is the student name?");
if (name === "John" || name === "Penny" || name === "Pat") {
console.log(name + " has one free hour of lessons.");
} else {
console.log(name + " doesn't have any free lessons.");
}
As noted in the comments, this gets difficult when you add more users. Instead, let's store the users who have a free hour in an array (thus, adding new students is easy, just add them to the array):
var haveFreeHour = ["John", "Penny", "Pat"];
Then, we can use indexOf
in the if statement:
var haveFreeHour = ["John", "Penny", "Pat"];
var name = prompt("What is the student name?");
if (haveFreeHour.indexOf(name) > -1) {
console.log(name + " has one free hour of lessons.");
} else {
console.log(name + " doesn't have any free lessons.");
}
Play with this at JSFiddle