jeudi 5 août 2021

How to write a javascript to discern between specific guests for a "guest list" variable array

So this isn't anything crazy as I am just starting out with coding. I just learned about if else statements and then variable arrays. I made a "guest list" code that will either welcome the guest or tell them to leave, if their name is in the variable array. I have that down fine. But I'm trying to make it so that one specific person when prompted for their name will be greeted differently than the others, without it also executing the other code. I've gotten it to work, but it'll say "hello, sir!" and then say "you can't enter!" right after. The snippet I have attached is the base working idea, and the image shows what I want to add. I'm just really trying to get down these if else statements because I think these will be really helpful in the long run. I effectively want this to recognize the name, then if the name is on the normal list, greet them. If it's on the owner list, respectful greeting. And if on neither, tell them to leave, without double responses.

var guestList = ["John", "Tony", "Mark", "Jones", "Mike"];
var ownerList = ["Cole"];


var guestName = prompt("What is your name?");

//This is the code I'm trying to add 
if (ownerList.includes("guestName")) {
  alert("Hello, Sir!")
}

if (guestList.includes(guestName)) {
  alert("Hello, and welcome!")
} else {

  alert("You shall not pass!");
}

Aucun commentaire:

Enregistrer un commentaire