Needing help on the Magic 8 Ball project in JavaScript

I believe my code is correct but what is printing out to the console is confusing me. Below I will show what my code is and what is printing out to the console with that code.

Here is my code

let userName = ‘Alex’;

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

let userQuestion = ‘Will I be a millionaire one day?’;

console.log(‘${userName} has asked - ${userQuestion}’)

let randomNumber = Math.floor(Math.random() * 8);

console.log(randomNumber);

let eightBall = ‘’;

switch (randomNumber) {
case ‘0’:
eightBall = ‘It is certain’;
break;
case ‘1’:
eightBall = ‘It is decidedly so’;
break;
case ‘2’:
eightBall = ‘Reply hazy try again’;
break;
case ‘3’:
eightBall = ‘Cannot predict now’;
break;
case ‘4’:
eightBall = ‘Do not count on it’;
break;
case ‘5’:
eightBall = ‘My sources say no’;
break;
case ‘6’:
eightBall = ‘Outlook not so good’;
break;
case ‘7’:
eightBall = ‘Signs point to yes’;
break;
}

console.log(‘The Magic 8 Ball says, ${eightBall}.’)

Here is what prints out to the console

Hello, ${username}!
${userName} has asked - ${userQuestion}
4
The Magic 8 Ball says, ${eightBall}.

Can someone please explain why the I cannot get the intended strings to print out?

You are using the wrong kind of quotation marks.
You should be using ` instead of ' for those string literals (the stuff with the ${variableHere} ).

So
console.log('The Magic 8 Ball says, ${eightBall}.')
should be
console.log(`The Magic 8 Ball says, ${eightBall}.`)
and so on.

2 Likes

Just for further clarification, that character @janbazant1107978602 is referring to is the backtick. It shares with the ~ tilde key next to 1.

For regular strings, you can just use regular apostrophes 'string' or quotation marks "string".
For literals, use must use the backticks `string with ${variable}`

1 Like