# Magic Eight ball Help

The code that user factoradic posted. Here’s mine for good measure.

``````let userName = 'Buzz';

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

let userQuestion = 'Will I ever return to the moon?';

console.log(`\${userQuestion}`);

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

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 eight ball answered:
\${eightBall}`);

``````

Your point on the global scope of `eightBall` is well taken, thank you. I understand why the exercise didn’t want to overload us with concepts we may not be familiar with yet, but it’s still good to know why a piece of code is necessary.

3 Likes

I thought that if we wanted to always print our ‘Hello, Jane!’, it was needed to use: `var userName = '' userName ? console.log('Hello, Jane!') : console.log('Hello!');`

instead of : `'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');`

``````'' ? console.log('Hello, Jane!') : console.log('Hello!');
// Hello! <- because empty string is considered false

'Jane' ? console.log('Hello, Jane!') : console.log('Hello!');
// Hello, Jane! <- because 'Jane' is non-empty string
``````

What is your question?

1 Like

Here is my JAVA solution, I decided to make it much more complex and feel like an actual eight ball rather than it just doing 1 function once. If you are confused please reply to this post, If you are wondering why nothing is in the constructor that is because nothing needs to be as I utilized scanner which takes in user input as well as Thread.sleep which ill be happy to explain you ask. I didnt do this in the codeacademy code editor btw I used eclipse IDE. I HIGHLY recommend running the program to see how it works. Thank you. Also when you are running it, yes there is only 2 outcomes but I was just too lazy to put in more, you can always edit and put your own outcomes in the String array “ballOutcomes” as well as changing how many numbers that can be randomly generated in the getRandomNumbers method. If you are wondering why if you type in the same question and get a different outcome, that is just how magic 8 balls work :P.

Hi,
What about this solution? It is different but it still works:

``````let userName = "";
userName ? console.log(`Hello \${userName}`) : console.log('Hello!');

let userQuestion ="Will I be a celebrity?";
console.log(`The user asked: \${userQuestion}`);

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

const eightBall = randomNumber;

switch (eightBall) {
case 0:
console.log('It is certain');
break;
case 1:
console.log('It is decidedly so');
break;
case 2:
console.log('Reply hazy try again');
break;
case 3:
console.log('Cannot predict now');
break;
case 4:
console.log('Do not count on it');
break;
case 5:
console.log('My sources say no');
break;
case 6:
console.log('It is certain');
break;
case 7:
console.log('Outlook not so good');
break;
case 8:
console.log('No way');
break;
}
``````

The below code is the code created per the instructions, and works perfectly fine:

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

const userQuestion = ‘Will I become the Software Engineer within this year?’;
console.log(`You\'ve asked: \${userQuestion}`);

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

let eightBall = ‘’;
console.log(`The eight ball answered: \${eightBall}`);

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

console.log(eightBall);

1 Like

The question is that

``````'' ? console.log('Hello, Jane!') : console.log('Hello!');
``````

makes no sense because anyone who adds a string would be called Jane! So how is this meant to be written?

The answer is Template Literals.

``````userName ? console.log(`Hello, \${userName} !`) : console.log('Hello!');
``````
``````const userName = ''
userName ? console.log(`Hello \${userName}`) : console.log('Hello');

const userQuestion = 'Will I become a werewolf tonight?';
console.log(`The user \${userName} asked : \${userQuestion}`);

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

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';
}

console.log(`The eight ball answered: \${eightBall}`);
``````

this is how i wrote mine and it seem to work

Nice work.

Consider:
``````console.log(userName ? `Hello \${userName}` : `Hello`);
``````

or

``````const userName = 'Herbert';
console.log(`Hello\${userName ? `, \${userName}` : ''}!`);
``````

Output:

Hello, Herbert!

without a name:

``````const userName = '';
console.log(`Hello\${userName ? `, \${userName}` : ''}!`);
``````

Output:

Hello!

``````console.log(`The user ased: \'\${userQuestion}\'`)

const randomNumber = Math.floor(Math.random * 8)

let eightBall = 'none';

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(eightBall)
``````

the value of eightBall doesn’t change so I end up with an empty string instead of an eightBall reply

I got it
I used Math.random instead of Math.random()