Peter Carter
Peter Carter

Reputation: 2728

Syntax error in JavaScript switch statement

Coding an online rpg and I've created case statements in Javascript for the story paths. However, when I run the code I get the following error: "Uncaught SyntaxError: Unexpected token ILLEGAL" for line 26, which is the line after the case statement: case "HotelStay":. I can't see anything wrong with the code, but when I take that and all the case statements below it out (except default:) the code works fine. Any ideas?

function onStart()  {
     document.getElementById("storybox").innerHTML = "<p><em>You are Drogon Barre, aka Dragon-Bear.</em></p><p>The date is October 3rd. You are sitting quietly at your father's house when a letter arrives through the door with details of a rape and murder. The letter says that the murder was committed by a member of the infamous cult <strong>The Order of the Mouse</strong>. The writer claims the victim was her sister and that she looked on powerless as the assailant took her sister's life. She claims the perpetrator currently resides in a hotel just outside Plymouth. The letter gives a time and an address but no further details.</p><p>Do you choose to investigate?</p><button type='button' class='btn btn-success' id='yesbtn' onclick='buttonClicked(\"YesStart\")'>Yes</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>No</button>";
};

function buttonClicked(buttonid)  {
  switch(buttonid) {
    case "YesStart":
        document.getElementById("storybox").innerHTML = "You take the train down to Plymouth and meet at the location specificied. Initially the woman fails to notice you and stares into the distance with the dazed look of someone who has considered the exact point at which the universe might have ended, present within a kind of altered dimensionality that places her materially at a similar point of existence to you, while leaving her utterly absent from it in some other, more absolute sense. Two nuns scurry past her, heading towards a small, fluffy dog, who they pet while making cooing sounds; a stern man in an ill-fitting police officer's uniform eyes them suspiciously. This spectacle seems to rouse her from her trance, and she looks quizzically at them, tilting her head to one side, before spotting you out of the corner her eye and waving you over.</p><button type='button' class='btn btn-success' id='continuebtn' onclick='buttonClicked(\"ContinueStart\")'>Continue</button>";
        break;
    case "NoStart":
        document.getElementById("storybox").innerHTML = "<p>You try to fall asleep in your bed but you cannot. Your heart is beating so fast you feel like it will fail. All night you are plagued by images of your still conscious body being cut open at autopsy. You wake up often. During the days you are afraid to be left alone. The only solace you have is a lady who calls herself Deer-Wolf, who texts you often and calls you on occasion. You find her voice comforting but you are still afraid, and feel that you will never be fully safe until you solve the case. You ask your friends for help, even offer to pay them to investigate but they either ignore you, decline, or tell you to go away. You are not sure if they believe you and are too scared, or if they think you are crazy. Either way, they won't help.</p><p>Are you sure you don't want to investigate?</p><button type='button' class='btn btn-success' id='yesbtn' onclick='buttonClicked(\"NoStart\")'>No</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"YesStart\")'>Yes</button>";
        break;
    case "ContinueStart":
        document.getElementById("storybox").innerHTML = "<p>She takes you to a hotel. You chat a little on the way and she says her name is Deer-Wolf, which is the name of the lady you have been texting recently. You ask her about it and she tells you that yes, it was her who was sending the text messages. She is acting very unusually, speaking only when directly addressed and seemingly either angry with you or mired in a depression that makes your presence irrelevent. You suspect something of the two. When you arrive the hotel is empty but for two bleary eyed reception staff who stare as you walk past. Deer-Wolf tells you this is the place where the murderer lives. He rents a different room each week, always under different assumed names. He tells people this is because he is married, and likes to take women back unnoticed. The staff have never seen one of these women leave, but the room is always impeccably kept, and the uneasy feeling the hotel staff have about him has never yet been officially corroborated.</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>";
        break;
    case "HotelStart":
        document.getElementById("storybox").innerHTML = "<p>You investigate the hotel, but Deer-Wolf won't tell you the room number and you think you see the faint glimmer of a smile when you are about it. She tells you that you should talk to some of her friends who run a nudist group on the internet. They live in Swindon and she has invited you up so see them. You suspect she is playing a game with you. You could stay in the hotel on more night, or go investigate the group in Swindon Deer-Wolf suggests. Where will you go?.</p><button type='button' class='btn btn-success' id='mapbtn' onclick='buttonClicked(\"Map\")'>Go to Map</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelStay\")'>Stay in Hotel</button>";
        break;
    case "HotelHelp":
        document.getElementById("storybox").innerHTML = "<p>The only help you have at the moment is Deer-Wolf.</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>";
        break;
    case "Map":
        document.getElementById("storybox").innerHTML = "<p>Map Loads Here</p><button type='button' class='btn btn-success' id='investigatebtn' onclick='buttonClicked(\"HotelStart\")'>Investigate</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Get Help</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"NoStart\")'>Go Home</button>";
        break;
    case "HotelStay":
        document.getElementById("storybox").innerHTML = "<p>You stay overnight in the hotel. Deer-Wolf knocks at your door in the night. You go to open it and find her naked. She invites herself into your room.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"Invite\")'>Invite her in</button><button type='button' class='btn btn-warning' id='kissdeerbtn' onclick='buttonClicked(\"KissDeer\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that doesn't mean you
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    case "Invite":
        document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    case "Ask":
        document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;
    default:
       console.log("That part of the story hasn't been written yet.");
    };

};

onStart();

Upvotes: 2

Views: 495

Answers (4)

user5564294
user5564294

Reputation:

Just by copy and pasting your code to sublime will do the trick. You have a new line feed in KissDeer case.

can do what you want.

Make them in one line and your code is fixed.

case "KissDeer":
    document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
    break;

Upvotes: 0

Downhillski
Downhillski

Reputation: 2655

you should add a \ to the end of kissDeer condition, see code below.

case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you\
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;

Upvotes: 3

john dith
john dith

Reputation: 51

Try this: Your string was not closed on the kiss deer case, I simply broke it up into two parts.

case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you"
        + "can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";
        break;

Upvotes: 1

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

There is problem in this line

case "KissDeer":
        document.getElementById("storybox").innerHTML = "<p>You try to kiss her but she pushes you away.</p><p>'Did you think because I'm naked I'd be fair game? No thanks. I'll just have a sit down and a gin and tonic if you're offering.'</p><p>Deer-Wolf sits down naked on the sofa of the hotel room she has rented for you.</p><p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, but that's doesn't mean you
    can do what you want. Hands to yourself or we forget the whole thing.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";

can do started from new line .

Join it with his string

LIke this

document.getElementById("storybox").innerHTML = "<p>Deer-Wolf thanks you, and walks past your into the hotel room, gazing fixedly into your eyes as she does so.<p>'It's a nice place. Double room. I'll be sleeping here too of course. It's got good security and I like the managers here. We get on well.'</p><p>Deer-Wolf gets up and begins to walk the space between you in a semi-circle, as if stalking her prey.<p>'I always sleep naked, I hope that's not a problem.</p><button type='button' class='btn btn-success' id='invitebtn' onclick='buttonClicked(\"HotelStart\")'>Invite her in</button><button type='button' class='btn btn-warning' id='helpbtn' onclick='buttonClicked(\"HotelHelp\")'>Try to kiss her</button><button type='button' class='btn btn-danger' id='nobtn' onclick='buttonClicked(\"Ask\")'>Ask how the Swindon group can help with the murder</button>";

JSFIDDLE

Upvotes: 1

Related Questions