vendredi 28 avril 2017

How to call a string array in a if/else statement in Javascript

I'm a beginner in javascript and I trying to call a string array in an if/else statement. This is what I have. Which works fine, but is there a shorter way to execute this. I don't want to use any other method. I just want to stick to if/else statement.

enter code here

    var welcomeMessage = prompt("what is your name?").toLowerCase();
    var names = ["brad", "elena", "simon", "stella", "stasia", "sylvester", "sinclare"];

    if(welcomeMessage === names[0] || welcomeMessage === names[1] || welcomeMessage === names[2] || welcomeMessage === names[3] || welcomeMessage === names[4] || welcomeMessage === names[5] || welcomeMessage === names[6]) {
    alert("Your Authorized!");
  } else {
    alert("Signup for free!");
}

Aucun commentaire:

Enregistrer un commentaire