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.
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!');
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;
}
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;
}
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}`);
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
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}`);
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}`);```
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}`)
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}`);