Error comparison operators javascript [closed]

-4

I've already tested here and this variable returns true, even though the operands are the same, in this case it was to return false and jump to else .

function testar(){
    var isOk =  /[A-Z][a-zA-Z][^#&<>\"~;$^%{}?]{1,20}$/;
    var nome = "Francisco";




    if(nome == ""){
        console.log("coloque o nome ")
    }else  if (nome != isOk.test(nome)){
        console.log("nome errado amiguinho");
    } else {
        console.log("ok vá em frente");
    }


}
    
asked by anonymous 03.10.2018 / 00:04

1 answer

1

I think what you want to do here is this:

    var isOk =  /[A-Z][a-zA-Z][^#&<>\"~;$^%{}?]{1,20}$/;
    var nome = prompt("Digite o nome:");

(function testar(){
    if(!nome) { //se não for digitado nada...
        console.log("coloque o nome ")
    }else  if (!isOk.test(nome)) { //se o nome NÃO bater com o regex...
        console.log("nome errado amiguinho");
    } else { //acertou!
        console.log("ok vá em frente");
    }
})();

At line if(!nome) , if the person does not enter any value, the value "" is considered false . The ! will invert and make the same true , and run the code, asking you to type something. If any value is filled in, the filled string will count as true , and when inverted it will false , jumping to else if .

In the else if (!isOk.test(nome)) part, test will return true if the name entered is validated by the regex, and then inverted by the exclamation. That is, if the name is NOT validated by the regex, erroneous answer. The last else will respond unlike the previous if, ie if the name is correct.

There are better ways to do this, but I've edited your code as little as possible to make it functional.

    
03.10.2018 / 15:04