Checked similar topics, still getting the error I need a break after each case


#1

I know this is similar to other topics, but I have tried what you said in those topics and I am still getting the error.

I made sure I do not have long response strings, I do not use the keywords 'case' or 'break' for anything other than each case and a break after each one. I have removed all comments in this section, even though they made the code more self documenting. Originally I left the break off cases that were going to do the same thing, but added them in the first time I received the error about needing a break for each case. I have checked everything I can think of, and the topics I could find on this, but I am still having the problem. Here is my code as it stands now. It is working as I intended, but the code checker isn't satisfied. What is the code checker not liking?

var haveRead = true;
switch(book)
{
case 'The Hobbit':
alert("Tolkien was a genius.");
break;
case 'The hobbit':
alert("Tolkien was a genius.");
break;
case 'the Hobbit':
alert("Tolkien was a genius.");
break;
case 'the hobbit':
alert("Tolkien was a genius.");
break;

    case 'A Stranger at The Wedding':
        alert("I really like Barbra Hambly");
        break;
    case 'Time of the Dark':
        alert("I really like Barbra Hambly");
        break;
    case 'Walls of Air':
        alert("I really like Barbra Hambly");
        break;
    case 'Armies of Daylight':
        alert("I really like Barbra Hambly");
        break;

    case 'Black Trillium':
        alert("Andre Norton was a very talented writer.");
        break;
    case 'Dare to Go A-Hunting':
        alert("Andre Norton was a very talented writer.");
        break;
    case 'Shadow Hawk':
        alert("Andre Norton was a very talented writer.");
        break;

    default:
    alert ("I haven't read that one.");
    haveRead = false;
}

#2

Does the code checker give a reason for not accepting the code? Which exercise is it?


#3

I'm pretty sure the exercise gives you the names and things you're mean't to input into the switch

follow the exact instructions using the exact text they give you.


#4

Also a hint, if you want the same answer for multiple cases you could use:

case 'The Hobbit':
case 'The hobbit':
case 'the Hobbit':
case 'the hobbit':
            alert("Tolkien was a genius.");
            break;

then you go on to the next case until you reach a break which would in this case be what you need.
Or if it's about case sensitivity think about .toUpperCase() or .toLowerCase().


#5


bobuou: The code checker says I need a break for every case.

benjne: As the picture shows. this was supposed to be a free exercise. No instructions, other than at least 3 case statements and a default, which is why I was surprised to get the error.

haxor789: I originally used the format you suggested for each author. I reformatted my code to to the more complex version you see after the first time I received the error stating I needed a break after each case. I still received the error, so I double and triple check to make sure I had a break after each case, that all colons and semicolons were present, that I wasn't missing a curly brace, etc. No dice, still received the error. I checked the forums and came across a similar entry stating that the other student was having problems because they had really long console.log() statements. I am using alerts, but I did have a really long one, so I clipped it short. No dice. I removed some comments, in case they were tripping up the code checker. No dice. I saw another entry where the student was told they couldn't use the words "case" or "break" anywhere in their code besides "case:" and "break;". I was sure I didn't have any, but checked anyways. My code was free of that potential problem. Still getting the error. Not sure what to try next. My code has worked from the original through every revision, but the error won't let me proceed.


#6


This is the error I get, even though my code has a break after each case (but doesn't need one to function properly), and it functions as intended.


#7

Ok that is weird the only thing I can see is questionable is that there is something missing on top:

var haveRead = true;
    switch(book)

as book is not declared and haveRead is not used elsewhere. Otherwise I'd suggest that you pass it with a really simple switch and do the more complex one e.g. here:
https://repl.it/


#8

Oh brother: I found the problem. I ignored a comment at the top of my code which had the word "case" in it at least once. I removed the comment, and the code checker accepted my work. I can't believe I missed checking that comment after I read how your code checker looks for ANY use of the words "case" and "break", then compares the counts of both and returns the error if they don't match :blush: In my defense, if this exercise is going to insist on checking the number of "case"s against the number of "break"s it really should tell us (and maybe let us in on the "do not use case or break for anything other than the code of the switch statement" rule).


#9

Sorry haxor789, I didn't have the screen shot rolled up high enough for you to see that I placed my switch statement in a function, with the argument "book", and had it return haveRead as a boolean check for an if/else check after the function call.

By the way, thanks for the cool course, I'm having fun :slightly_smiling: