Why do I keep getting undefined in addition to the expected output?

After solving the exercise on this challenge here, my code works fine. However, in addition to the expected result of the code, I keep getting Undefined below it. What could be the problem?

My code is below

const toEmoticon = beep => {
  switch (beep){
    case "shrug":
      console.log('|_{"}_|');
      break;
    case "smiley face":
      console.log(':)');
      break;
    case "frowny face":
      console.log(':(');
      break;
    case "heart":
      console.log('<3');
      break;
    default:
      console.log('|_(* ~ *)_|');
      break;
  }
};

Your code is working I think…

I just tried in the console

console.log(toEmoticon("shrug"))
//ouput: |_{"}_|

It’s because you’re meant to return the emoticons, not log them.

You’re essentially double logging.

console.log(toEmoticon("whatever")) 

This is logging the output of the function, but your function doesn’t return anything so returns undefined

1 Like

In addition to your expected output, did you not get “Undefined” when you tried it?

Ah yes @munroe4985 is right. Don’t double console.log…

1 Like

Yes! Awesome.

That solved it.

I either changed all the individual console.log()s within the Switch statement to return.statements

OR

I removed the final console.log() below , which called the function and then changed it to simply call the function rather than logging it.

Thanks for your sharp observation and input.

1 Like