So while playing around with some js code, I came across something interesting. The problem statement goes like-
Given a string, use switch case to evaluate the following conditions:-
If the first character in string is in the set {a, e, i, o, u}, then return A.
If the first character in string is in the set {b, c, d, f, g}, then return B.
If the first character in string is in the set {h, j, k, l, m}, then return C.
If the first character in string is in the set {n, p, q, r, s, t, v, w, x, y, z}, then return D.
And I KNOW that this is not the best implementation, so don't @ me.
function getLetter(s) {
let letter;
switch(s[0]){
case 'a'||'e'||'i'||'o'||'u':
letter='A'
break
case 'b'||'c'||'d'||'f'||'g':
letter='B'
break
case 'h'||'j'||'k'||'l'||'m':
letter='C'
break
case 'n'||'p'||'q'||'r'||'s'||'t'||'v'||'w'||'x'||'y'||'z':
letter='D'
break
}
return letter;
All cases except the last one work fine. In the last case, the value of 'letter' is set to 'D' only if the string begins with 'n' & not for any other characters in the case label. Why is this happening? Genuinely Curious.