My switch statement on Code Your Own Adventure 2 only gives me the default output


#1



I am working on the Code Your Own Adventure 2 project, and I am on the part where I must add a switch statement to my to my prompt. The prompt came up and I typed in one of the cases I has set up, however when I entered it in, the default output came up. I tried again with a few other cases I had set up, but the result was the same. Then when the prompt came up, I pressed cancel, and a message saying "TypeError: Cannot read property 'toUpperCase' of null" was displayed. I tried the same thing a few months back before I became frustrated and gave up, except the question I asked was where they would like to work, and the same thing happened. When I inserted the input and the wrong output came out, the assignment was still checked of as a sucess, but I would like to know why my program isn't functioning properly. Any help would be greatly appreciated.


Replace this line with your code. 

var user = prompt("What is your favorite thing to do?").toUpperCase();

switch(user) {
    case 'read':
        console.log("I like to read too.I like to imagine different ways that the story could happen.");
    break;
    
    case 'swim':
        console.log("I enjoy swimming too, but I don't really get to do it that often. The YMCA that my family used to swim at closed down so there aren't any places to go swimming that are near.My Pa Pa Reado has a swimming pool but my sister and I only go when my dad is in town."); 
    break; 
    
    case 'watch tv':
        console.log("I like it too, but I go on the computer more now. I like to watch Steven Universe, Everybody Hates Chris, We Bare Bears, Star vs the Forces of Evil, and some others. Just make sure that you don't watch too much.");
    break;
    
    case 'code':
        console.log("Me too, though i'm sure that pretty obvious. I gave p on it a while back because one of my rograms wouldn work but it seems like lately eveyone wants to be a coder but just thrilling for me to learn something new and be able to apply it like I am now. I decided to pick it back up recently and im having a great time");
    break;
    
    case 'run':
        console.log("It's kinda fun for me to push myself at school and set goals for my running but Idot really excercise much in my free time. I do get random bursts of energy at times and I just run around like crazy for a few seconds.");
    break;
    
    case 'sleep':
        console.log("Well that's not really doing anything but you know what they say, different strokes for different folks. Plus, I like to sleep too.");
    break;
    
    case 'play video games':
        console.log("Did you know that people who play video games have better attention to detail and slowed brain aging? Plus, you're probably more likely to survive a zombie apocalypse.");
    break;
    
    case 'dance':
        console.log("I dance sometimes for fun. I have a sister who dances at her college.");
    break; 
    
    case 'sing':
        console.log("I did karaoke at my school on the last day before winter break started. I was shaking the whole time.");
    break;
    
    case 'listening to music': 
        console.log("I listen to music all the time. Lately i've been listening to this song called Virtual Riot - Idols(EDM Mashup). It really cool you should check it out.");
    break; 
    
    default: 
        console.log("I wasn expecting that but it sounds awesome.");
}


#2

That tells you exactly what happens on pressing cancel. Deal with the null from prompt, or don't press cancel.

And if you try to match an upper case string to lower case ones, then you'll get no match. If you're not sure which value you are running the switch on, then you can print it out to find out, and if there's no case that matches it then.. it's not going to.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.