mercredi 19 août 2020

Abbreviate check function

I'm just starting to learn javascript and i'm trying to figure out how to make this code shorter. Right now, the check function evaluates if a = "admin" and after that if a = "manager". Is it possible to do this evaluation in one line? Kind of "if (a = "admin" or "manager") ..."

const valid = "User name valid";
const invalid = "User name invalid";
function check(a, b) {
  if (a === "admin") {
    return valid;
  } else if (a === "manager") {
    return valid;
  } else if (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
    return valid;
  } else {
    return invalid;
  }
}
console.log(check("manager", "ikey"));
console.log(check("admin", "root"));
console.log(check("user", "ikey"));
console.log(check("user", "Mikey"));

Thanks!!

Aucun commentaire:

Enregistrer un commentaire