Magic Eight Ball coding exercise

*Hi all. I am struggling with the early stages of the Magic Black Ball exercise.
My line of code is: userName ? console.log(“Hello, ${userName}”) : console.log(“Hello!”);

the output I get is: "Hello ${userName}
but comparing my code to the video help I cannot see the error.
Any help woul be much appreciated, thank you.

You have three kinds of quotes. The " and ' can usually be used interchangeably. However the little guy under the ~ is special: `.

(can’t put it in hightliging because it is the ■■■■■■ hightlight character for this editor.

So:

userName ? console.log("Hello, ${userName}") : console.log("Hello!");
vs.
userName ? console.log(`Hello, ${userName}`) : console.log("Hello!");

I kind of hate the two console.log thing and would probably go with:

console.log(userName ? `Hello, ${userName}` : "Hello!");

The ternary should resolve to a value, rather than be flow control.

Thank you for your response. However, Having changed the quotes to singles (on @ key),the resultant output is still the same. The double-quotes automatically ‘materialised’ when I happened to click on the Format Code icon.
reducing the console.log to just the first so I now have:

userName ? console.log(‘Hello, ${userName}’) : (‘Hello!’);

and I still get the output of: Hello, ${userName}

I assume if I don’t resolve this but complete the rest of the exercise I will never get the ‘Next’ button active?
Hope you can help me through this
thanks again
Barry

For template literals, we use backticks (the key with the tilde ~. It is next to the 1 key on US Qwerty keyboards in the top left corner):

// Backticks: correct
console.log(`${userName}`);

// Quotes (single or double): incorrect
console.log('${userName}');
console.log("${userName}");  

If you still can’t find the backtick key on your keyboard, mention the model of your keyboard (or a link to similar keyboard). If the keyboard uses a different layout, then the backtick key may be in a different position (e.g. a user with a Norwegian laptop keyboard had a different layout and had the backtick in a different position). The position of the backtick key will depend on your keyboard’s layout.

Aha!!! So simple isn’t it? Could not see that subtle difference in the example code - old-age eye-sight :cry:
Thank you so much. I was close to abandoning JS!!!

I’m using an English qwerty KB back-tick is still next to ‘1’ key (but not ~).
Regards
Barry

1 Like

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