3

I have code that defines behavior based on which input the code is handeling at that moment. The code looks something like:

switch(inputOption.name) {
 case 'NAME_1':
  switch(inputOption.type.toLowerCase()) {
    case 'radio':
      //some code
    case 'text':
      //some code
    ...
    case 'image':
      //some code

    default:
      return inputOption.value;
      break;
   }
  break;

  default:
    break;
 }

The code also includes some cascading cases. The default option causes the error. The error is listed as

    the default case is already defined

What is causing this error? The error shows up on the package folder, but the file shows no errors in the package view, but it shows errors when I open the file. I assumed it had something to do with the second default declaration, but it had no effect removing it.

13
  • Which browser is this? Because it's most certainly a bug, the labels should be local to their scope. Commented Oct 16, 2014 at 22:32
  • No you're not. Commented Oct 16, 2014 at 22:33
  • Not a browser error, it's showing as an error in eclipse itself. Commented Oct 16, 2014 at 22:33
  • Is it an error or a warning? I can understand the second (a missing break is usually a bug, or at least 'smelly code'), but not an error. That's also why the browsers don't complain. Commented Oct 16, 2014 at 22:34
  • It's listed as an error in eclipse. I'm pretty sure the browser isn't complaining, the submission is working as intended, I just don't understand why eclipse itself is telling me the default case has already been defined. Commented Oct 16, 2014 at 22:36

1 Answer 1

0

You miss break statement for your outer case 'NAME_1'

switch(inputOption.name) {
 case 'NAME_1':
  switch(inputOption.type.toLowerCase()) {
    case 'radio':
      //some code
    case 'text':
      //some code
    ...
    case 'image':
      //some code

    default:
      return inputOption.value;
      break;
   }
   break; // <-------------------------------------------------- ADD THIS

  default:
    break;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

It may be a solution, but JS most definitely supports fallthrough in switching - his code should be allowed.
Sorry, I meant to be more clear, there is that line included in the file. I couldn't directly copy-and-paste, as this is work code.
@MaksymKozlenko the original code works too, fyi. that's not the point.

Your Answer

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.