Where exactly is the issue?

Could someone please help me understand where the issue is here in the code? After a few failed attempts I did some rather extensive research and learned quite a bit about switch statements.To be honest, I don’t see how I had my code wrong initially but that is now besides the point. What I would like to understand is how is my code wrong? As far as I can see it is identically the same as to how it should be. Could someone please help me out here?


function switchOfStuff(val) {
let answer = "";
// Only change code below this line
switch (answer){
case "a":
    answer = "apple";  
    break;
case"b":
    answer = "bird"; 
      break;
case"c":
    answer = "cat"; 
      break;
default: 
    answer = "stuff"; 
      break; 
} 
// Only change code above this line
return answer;
}

switchOfStuff(1);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36

Challenge: Adding a Default Option in Switch Statements

Link to the challenge:

What is the value that you want to check?

I don’t understand? Value, no… I just keep getting an error message when I run the code in the challenge.

This is switching on the value stored in answer.

I just keep getting an error message when I run the code in the challenge. Do you see the error in my code man? For the life of me, I just don’t see it. It seems the issue is with the return statement. At least, that is the error that I keep getting when according to me the code is exactly the way it should be.

Do you see the problem here?

1 Like

I am telling you the error… Did you just not see my entire last post?

The error is that you are switching on the value stored in answer.

Write a switch statement to set answer for the following conditions:
a - apple
b - bird
c - cat
default - stuff

You need to set the value stored in answer. You therefore can’t use the variable answer as the value you are switching on. That doesn’t make any sense. The switch statement executes code based upon the case for the value stored in the variable you are comparing against, given at the head of the switch statement.

I’m running the function, man. In the bottom of the code. Check it out. I am defining what’s going to get passed in from outside by calling it…

Ok. You are absolutely ignoring what we’re telling you. I can do absolutely nothing to help.

1 Like

Maybe do some more independent reading on how switch statements work? You need to understand what the following is doing:

2 Likes

How is the outside value called inside the function?

I also think your logic code is on point but there is an oversight (which happens a lot).

javascript-switch-case-js-switch-statement-example

function switchOfStuff(val) {
    let answer = "";

    // Only change code below this line
    switch (answer){
    case "a":
        answer = "apple";  
        break;
    case"b":
        answer = "bird"; 

The switch statement takes a switch statement takes a switch-case, switch(case) and several cases and a default case:

case val:
    // code
    break;
...
default:
    // code
    break;

function switchOfStuff(val) has val an a parameter that would be passed as the switch-case but answer with a default value of an empty string was passed, let answer = "";.

So pass val instead.

function switchOfStuff(val) {
    let answer = "";

    // Only change code below this line
    switch (val){
    case "a":
        answer = "apple";  
        break;
    case"b":
        answer = "bird"; 
2 Likes

Try and put space between (answer) and {

Check the changes I made to your code and see if that works.

MOD EDIT: SOLUTION REDACTED

switchOfStuff(“c”); //I replaced 1 with “c”. This is the value you send as parameter when you call the function. Such value will be evaluated for the switch statement and return a string (stored in the variable “answer”) according with the value you send when you call the function : “a”, “b” or “c” or other value(or in this case letter) you send to the function, such value will be stored in parameter(or variable) “val”, Hope this can help you a little. Good luck.

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Hey there bud! Thanks for taking your time to reply with a rather helpful answer. Indeed, thanks. I got past this challenge a few days ago but due to my workload and my daily responsibilities, it’s been hard to reply. I thank you wholeheartedly.

This was exactly what I was asking for. It was obvious there was a gap in my understanding of the topic and what was requested of me, I just could not piece it together, and it finally dawned on me: the name of the statement -the manner in which I had paired up the variables . Appreciate ya very much indeed.