Selecting from many options with Switch Statements not correct but passes the test

Selecting from many options with Switch Statements not correct but passes the test
0.0 0

#1

Tell us what’s happening:
I don’t really have a problem with the solution but I brought up a solution that is wrong and it is accepted as is.

Fact is that the break in this solution is unreachable because of the return value (instead of console.log).
I don’t know if I’m correct but I think this should not be marked as correct.

Your code so far

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch(val){
  case 1:
    answer = "alpha";
    return answer;
    break;
  case 2:
    answer = "beta";
    return answer;
    break;
  case 3:
    answer = "gamma";
    return answer;
    break;
  case 4:
    answer = "delta";
    return answer;
    break;
  default:
    answer = "only 1 to 4 please";
  }
  
  
  // Only change code above this line  
  return answer;  
}

// Change this value to test
caseInSwitch(1);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36.

Link to the challenge:


#2

There is nothing about your solution that is wrong or circumvents any of the tests. The break statement stops the switch statement from continuing checking cases. You added the return answer to each case before the break, which is fine even though it repeats code unnecessarily. If you removed each return statement, the final return at the end of the function would have returned the correct answer based on the break statements you put into your solution.

Whenever a return statement is used, the the value specified after the return is returned and the function immediately exits the function.


#3

I just find it weird that the code is accepted although it’s a bad coding way, that’s just my thought.


#4

When you say bad, why do you feel that way?


#5

You mentioned it yourself,
writing useless code is not a good coding habit.
Especially the fact that the editor itself is showing that there’s something wrong with the code, is enough reason not to give permission to the next question until the problem is resolved.
I know that the basics are met but it is still bad code.

But of course, that is only my opinion.


#6

I definitely did not say your code was useless. If it solves the challenge, then it is useful.

I said you have unnecessary code (the repeated return statements). Just because your code is not as concise as it could be, does not change the efficiency of your solution.

The FCC tests are only designed to detect a correct solution. They are not designed to detect the most concise code, because that would mean knowing all possible solutions and that would be impossible. The FCC tests do have a built efficiency test that you may discover when you get farther along in the curriculum. If a test takes too long to complete, then you will get a message that it thinks you may have an infinite loop.

For now, just worry about getting passing solutions. As part of the learning experience, you should go back to previous solutions when you have the time, and refactor them to make them more readable and concise. The ability to write concise code comes with practicing writing code and studying others’ solutions to the same challenge.

You can always post a question on the forum and ask for an opinion about your code’s readability, conciseness, and efficiency. Someone will almost always respond to such a question. Keep in mind that everyone will have a different opinion about readability and conciseness.


#7

Just finished this one and was wondering what the warning was about. Thanks @lawfets for asking, and thanks @randelldawson for your answer!