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.

4 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()

var userName = "Jane";

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

var userQuestion = "Let's create some random answer, don't you think?";

console.log(userQuestion);

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

var eightBall = "";

switch(randomNumber){
  case 0:
  eightBall ='It is certain';
  break;
  case 1:
  eightBall ='It is decidedly so';
  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}`); 

1 Like

Good day, I would like to ask about this exercise. When i run code i sometimes got no answer.

let userName = "Jane"; if (userName === 'Jane') { console.log('Hello, Jane!')} else { console.log('Hello!'); } let userQuestion = ''; console.log(userQuestion); const randomNumber = Math.floor(Math.random()*8); let ball = ''; 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: eihgtBall = 'Outlook not so good'; break; case 7: eightBall = 'Signs point to yes'; break; default: } console.log(`The eight ball answered: ${eightBall}`);

Sorry, i understood my problem, i should errase default parameter.

Does anyone know where I went wrong in this? I went through the lesson three times and have been working on this project for two days; I finally got it to stop giving me errors but it’s still not running how it should.

let userName = 'Lee'
userName ? console.log('Hello, ${userName}!') : console.log ('Hello!');
let userQuestion = 'Is death real?';
console.log(`${userQuestion}`);
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = ''
switch (eightBall) {
  case 1 :
  console.log('It is certain');
  break;
  case 2 :
  console.log('It is decidedly so');
  break;
  case 3 :
  console.log('Reply hazy try again');
  break;
  case 4 :
  console.log('Cannot predict now');
  break;
  case 5 :
  console.log('Do not count on it');
  break;
  case 6 :
  console.log('My sources say no');
  break;
  console.log('Outlook not so good');
  break;
  case 7 :
  console.log('Signs point to yes');
  default: 
  console.log('');
  break;
}
console.log(`The Eight Ball says ${eightBall}`);```

I see a problem with the switch statement:

it should be

switch (randomNumber) {

because you are using a number for the cases,

Also,
You have:

  case 1 :
    console.log('It is certain');
    break;

but later, you try to use the variable eightBall to show your result:

console.log(`The Eight Ball says ${eightBall}`);

You never changed eightBall to be anything but the empty string.

It might make more sense to do something like:

  case 1 :
    eightBall = 'It is certain';
    break;
1 Like

Never mind, I figured out my issue after tinkering around with it some more. I used else…if statements instead of switch statements and used instead of ' on [codebyte]console.log(Hello, ${userName}!’)[/codebyte]

Whenever I run this the eightball doesnt say anything and I cant seem to figure it out

let userName = '' userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!') const userQuestion = 'Will I live to 100?' console.log(`Will I live to 100, ${userName}?`) 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' break } console.log(`The eight ball said ${eightBall}`)

The problem might be that randomNumber is a number,
but the cases in the switch statement for it are strings.

maybe

case '0':

should be

case 0:

and so on.

1 Like

Well, I just start 3 days ago and solved it like this. and it worked. if anyone like me is new and stuck in this project. I hope I could help. Happy codding guys!

let userName = ''; userName ? console.log(`Hello, ${userName}!`) : console.log('Hello!'); const userQuestion = 8; console.log(`${userQuestion}`); 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'); break; } console.log(`The 8 Ball: ${eightBall}`);
1 Like