Problems with "IF"

0

I happen to be working with javascript and I have had to do this.

let ejemplo = "si";

if (ejemplo == "si") {
    //realiza 'x'
} else {
    //realiza 'y'
}

So far so good, if my variable ejemplo has the value "if" only performs 'x' and if it has a different value of "if" performs 'and'.

But when I make this change it does not happen the same

let ejemplo = "si";

if (ejemplo != "si") {
    //realiza 'x'
} else {
    //realiza 'y'
}

Any value that I give to ejemplo only performs 'x', in theory if I give "yes", I should do 'and', but that does not happen.

Any ideas?

    
asked by Angel Cayhualla 12.12.2018 в 22:21
source

1 answer

0

The problem could be in how the programming languages use the 'Strings', to buy strings we recommend methods of the same languages or third-party frameworks. In this case you could solve it using this

string_a.localeCompare(string_b);

The function will return

  

0: they are the same

     

-1: string_a < string_b

     

1: string_a > string_b

And your function would be:

let ejemplo = "si";

if (ejemplo.localeCompare("si") !== 0) {
    //realiza 'x'
} else {
    //realiza 'y'
}
    
answered by 12.12.2018 в 22:26