What your saying here is that you check what the function toEmoticon() is equal to. Since it will always be a function it will go to the default case.
If you want to use the input as the condition you would need to tell this to the function like so: let toEmoticon = (input) => {
after that you can use input as check in your switch condition.
switch(input) {
case 'shrug':
return '|{"}|';
break;
case 'smiley face':
return ':)';
break;
case 'frowny face':
return ':(';
break;
case 'winky face':
return ';)';
break;
case 'heart':
return '<3';
break;
default:
return '|(~)|';
}
Apologies if I am covering the same ground - but this code is printing the correct answers to the console - and yet the lesson checker is still not marking it correct. Do I have spelling errors somewhere I am not seeing?
This is the error message: If ‘shrug’ is entered, the function should return ‘| {“} |’
It indicates that shrug is not returning the correct response - but I copied and pasted that response from the brief so as to not get it wrong… colour me confused at this point!
Here is the code as I have it:
// Write your function here:
let toEmoticon = (input) => {
switch (input) {
case ‘shrug’:
console.log (’|{"}|’);
break;
case ‘smiley face’:
console.log (’:)’);
break;
case ‘frowny face’:
console.log (’:(’);
break;
case ‘winky face’:
console.log (’;)’);
break;
case ‘heart’:
console.log (’<3’);
break;
default:
console.log (’|(* ~ *)|’);
break;
}
};
// Uncomment the line below when you’re ready to try out your function
console.log(toEmoticon(“whatever”))
// Should print ‘|(* ~ *)|’
console.log(toEmoticon (“shrug”))
// We encourage you to add more function calls of your own to test your code!
I had return in there to start with but took it out because I thought that may be the issue - all the examples in the lessons gave console.log instead of return. I will put it back and try again…
Hi,
I seem to have everything correct, and I checked against what you have above, but I’m getting this error message: ** For any input other than ‘shrug’, ‘frowny face’, ‘winky face’, ‘heart’, or ‘smiley face’, your function should return ‘| ( ~ ) |’** can you suggest what I’m missing:
const toEmoticon = (input) => {
switch(input) {
case ‘shrug’:
return ‘|{"}|’;
break;
case ‘smiley face’:
return ‘:)’;
break;
case ‘frowny face’:
return ‘:(’;
break;
case ‘winky face’:
return ‘;)’;
break;
case ‘heart’:
return ‘<3’;
break;
default:
return ‘|(~)|’;
}
};
This is going to rock your mind, like it did mine. I took the inspiration from another member who voices code through expressions. Lo and behold, it works in JS too just thinking that way…
but then it’s turned out the easiest one EVER! Took me less than a minute!! I didn’t even have a single spelling mistake (for a change ) nor second attempt!!
You put ‘smiley face’ twice instead of making the second one ‘frowny face’:
That is why you are getting a different output when you try to log ‘frowny face’:
Did you add “const” before the function name at the top of your code?
Also, “default” at the bottom is misspelled:
P.S., the () around the strings in the cases are superfluous, as is using “break” in this example. Returning a value will exit the function, making “break” unreachable and therefore redundant.
Hello all, I am wondering why, in my switch/ case, I get the default answer and also ‘undefined’? I checked the solution, and although I see it adds the string as a parameter whereas I include it a string variable to take the expression to pass through the switch/ case, I thought the two might be interchangeable (string var in the function vs. param in the function). If anyone can help me understand why this is happening I would appreciate it!
const toEmoticon = function() {
let emoticon = '';
switch (emoticon) {
case 'shrug':
console.log('|_{"}_|');
break;
case 'smiley face':
console.log(':)');
break;
case 'frowny face':
console.log(':(');
break;
case 'winky face':
console.log(';)');
break;
case 'heart':
console.log('<3');
break;
default:
console.log('|_(* ~ *)_|');
}
}
console.log(toEmoticon('heart'))
// returns
|_(* ~ *)_|
undefined
undefined is the default return value when a function has no return of its own explicitly stated. Your code is logging at the caller, but it has nothing to log except undefined.
One possible cure is to log at the caller as you have done, but inside the function, return your case outcomes (and remove break).
I wrote similar to you, but without the breaks and used return instead of the console logs in the code. This below worked for me to move on.
const toEmoticon = (faces, emoji) => {
switch (faces) {
case 'shrug':
return ('|_{"}_|');
case 'smiley face':
return (':)');
case 'frowny face':
return (':(');
case 'winky face':
return (';)');
case 'heart':
return ('<3');
default:
return ('|_(* ~ *)_|');
}
};
console.log(toEmoticon("whatever"));
console.log(toEmoticon("heart"));
// |_(* ~ *)_|
// <3