-2

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.

asked Apr 10, 2021 at 12:51
3
  • 1
    The cases are checking the first operand of the logical or expressions only. Using lookup tables instead of switch would resolve this problem. Commented Apr 10, 2021 at 12:55
  • 1
    It's not about the "best implementation" here. Your implementation is incorrect. x || y is an expression, it is evaluated the same way in any context, including when you use it as a case label in a switch. Its value is the value of x if the value of x is true when it is evaluated as boolean or the value of y otherwise (no matter the value of y). This means the value of 'a'||... is always 'a', no matter what other values you put after the logical OR operator. Commented Apr 10, 2021 at 13:00
  • This question is similar to: What does the || (logical OR, double vertical line) operator do with non-boolean operands?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 20, 2024 at 9:56

3 Answers 3

0

Actually, getLetter('e') also returns undefined.

 switch(s[0]){
 case 'a': case 'e': case 'i':: case 'o': case 'u':
 letter='A'
 break
 case 'b': case 'c': case 'd': case 'f': case 'g':
 letter='B'
 break
 case 'h': case 'j': case 'k': case 'l': case 'm':
 letter='C'
 break
 case 'n': case 'p': case 'q': case 'r': case 's': case 't': case 'v': case 'w': case 'x': case 'y': case 'z':
 letter='D'
 break
 }

will work as you wanted.

Also, if there is a guarantee that s[0] is alphabet, you can rewrite:

default:
 letter='D'
 break
answered Apr 10, 2021 at 12:59
Sign up to request clarification or add additional context in comments.

Comments

0

This is not working for any of the other cases as well. it's only evaluating the first case.

If you want to use multiple cases you should do it in this format:

 switch(s[0]){
 case 'a':
 case 'e':
 case 'i':
 case 'o':
 case 'u':
 letter = 'A'
 break;
 case 'b':
 case 'c':
 ...
 ...
 letter = 'B'
 break;
 ...
 }
answered Apr 10, 2021 at 12:59

Comments

0

In JavaScript, a || b returns the value of a if it is truthy (this is commonly answered elsewhere on Stack Overflow). Since non-empty strings are truthy, this is what your switch statement effectively looks like:

 switch(s[0]){
 case 'a':
 letter='A'
 break
 case 'b':
 letter='B'
 break
 case 'h':
 letter='C'
 break
 case 'n':
 letter='D'
 break
 }

In other words, all the || bits are effectively made irrelevant, and your switch statement is really only accounting for four letters.

answered Apr 10, 2021 at 12:57

3 Comments

It's odd, that OP says: "All cases except the last one work fine", though.
@Teemu: Yeah, that's certainly not the case for me (no pun intended).
Thanks for the info; apparently all the test case strings start with the first the first char in each case label. My bad lol.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.