There is code, it has 3 variants of writing functions in order of validations - do you think which one is better?
"use strict";

const logins=["Mango","robot Googles","Poly","Aj4x1sBozz","qwerty123"];

const isLoginValid=function(login) {
  login.split("");
  if(login.length>=4&&login.length<=16) {
    return true;
  } else {
    return false;
  }
};

const isLoginUnique=(allLogins, login)=>! allLogins.includes(login);

const addLogin=function(allLogins, login) {
  //_______________
  //Option 1 =====================================
  //if(isLoginValid(login)) {
  //if(isLoginUnique(allLogins, login)) {
  //console.log("Login successfully added!");
  //return logins.push(login);
  //} else {
  //return console.log("This login is already in use!");
  //}
  //} else {
  //return console.log("Error! Login must be from 4 to 16 characters");
  //}
  //______________
  //Option 2 =====================================
  //if(! isLoginValid(login)) {
  //console.log("Error! Login must be from 4 to 16 characters");
  //return;
  //} else if(! isLoginUnique(allLogins, login)) {
  //console.log("This login is already in use!");
  //return;
  //}
  //logins.push(login)&&console.log("Login successfully added!");
  //__________________
  //Option 3 =====================================
  //if(isLoginValid(login)&&isLoginUnique(allLogins, login)) {
  //return logins.push(login)&&console.log("Login successfully added!");
  //} else if(! isLoginValid(login)) {
  //return console.log("Error! Login must be from 4 to 16 characters");
  //} else return console.log("This login is already in use!");
};

////function calls to check
addLogin(logins,"Ajax");//'Login added successfully!'
addLogin(logins,"robotGoogles");//'This login is already in use!'
addLogin(logins,"Zod");//'Error! Login must be from 4 to 16 characters'
console.log(logins);

1 Answers 1

Hello, the first thing to do is start with why you convert the string into an array? The string also has a length property.And your isLoginValid function can be easily rewritten to:
const isLoginValid=(login)=>login.length>=4&login.length<=16;


Well, by addLogin, then:
const addLogin=(logins, login)=>{
        if(isLoginValid(login)) {
                if(isLoginUnique(logins, login)) {
                        logins.push(login);
                        console.log("Login successfully added!");
        } else {
                        console.log("This login is already in use!");
        }
    } else {
                console.log("Error! Login must be from 4 to 16 characters");
    }
}