jeudi 7 juillet 2016

OR logical operator within an if statement returning truthy when falsey

function doesItStartWithJ (name) {
    if(name.charAt(0) === "J" || "j") {
        return "Hello " + name + "!";
    } else {
        return "Who are you?";
    }
}

I was trying to use the code above to solve a problem but kept getting errors on test cases with lowercase names and figured out that I had to fix my if conditional to make the code work, as such:

function doesItStartWithJ (name) {
    if(name.charAt(0) === "J" || name.charAt(0) === "j") {
        return "Hello " + name + "!";
    } else {
        return "Who are you?";
    }
}

I figured that I had to use the charAt method again for the lowercase cases. Why does this not work

"name.charAt(0) === "J" || "j""

but works with

name.charAt(0) === "J" || name.charAt(0) === "j"

???

Aucun commentaire:

Enregistrer un commentaire