7. more practice with switch


#1


https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/1/3?curriculum_id=506324b3a7dffd00020bf661


Oops, try again. Make sure to add at least one additional case to your switch.


var answer = prompt("Add your question here!");

switch(answer) {
  case '':
    console.log();
    break;
  // Add your code here!
  
}


#2

could you help me with this one @bibleman13


#3

of course give me one second! :smiley:


#4

okay. Thank you :wink: :wink:


#5

"We've given you the empty skeleton of a switch statement. Complete the existing case, then add at least one additional case and a default behavior with whatever console.log() calls you like." So in the previous two lessons you have looked at examples of switches. This lesson is now your first chance to "create your own" switch statement to show you have understood the basic workings of it. Lets take a look back at a shortened version of the lesson 6. practice with switch code!

switch(candy) {
  case 'licorice':
    console.log("Gross!");
    break;
  default:
    console.log("I enjoy that candy too!");
}

So in our switch statement we need to make two possible cases and add a default behavior at the end with a console.log of whatever we want. So lets follow what we have done in the previous lessons.

1.) Add your prompt and variable. In my lesson I created an exercise based code. So my prompt was how do you exercise? and my variable turned out to be exercise.

2.) Add your variable to your switch. So then I put exercise in my switch statement.

3.) Build the cases. So you only have to have two cases but you can do as many as you would like. Just follow the examples like from the previous lesson above to realize what all you need for each case. The case consists of a name (example for me "walking") a console.log statement to come back when that word is typed in ("keep walking!") and a break;

4.) Finally, add a default at the end in case your user inputs a word that you don't have in your case. Same as the cases except no word is needed. Once again you can reference your previous lessons for fully working examples of this.


#6

could you show me an example of the first step please? :slight_smile: :slight_smile: :slight_smile:


#7

var exercise = prompt("How do you exercise?");
switch(exercise) {

As you can see you have your prompt equaled to your variable. You then put your variable into your switch statement. So basically, you just need to decided what variable/answer you want and what question you want to prompt people to answer so as to get the words you put in your cases. The makeup of this is just like the skeleton they give you for this lesson, you just have to fill in the missing info!


#8

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