# Magic Eight ball Help

Ok, so I am kind of lost.

In the `switch` statement we want to assign random answer to the variable `eightBall`, right? `randomNumber` has a random value from `0` to `7`. If `randomNumber` has value `0` we want to assign `"It is certain"` to the `eightBall`, if value is `1` then `eightBall` should have value `"It is decidedly so"` etc.

So why you change the value of `randomNumber` here:

``````randomNumber = 'Do not count on it'
``````

? This value should be assigned to the `eightBall` variable.

In the `switch` we want to code the logic that sets value of `eightBall` based upon random value of `randomNumber`.

Ok, problem is in the `switch` value of `eightBall` should be dependent on the `randomNumber` value. So this `switch (eightBall)` should be changed to `switch (randomNumber)`.

So I did as you said and changed it

Edit: Also when I run it it does pick a random number and it changes the `randomNumber = 'Do not count on it'` to the other numbers

Ok, if we take your code from the first post:

``````switch (eightBall) {
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;
}
``````

and if we change `switch (eightBall)` to `switch (randomNumber)` we should get this:

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

The error is gone because we do not try to change the value of constant (`randomNumber`).

By looking at this discussion I think that problem is not with the syntax, but with understanding of the `switch`. It might be a good idea to revisit lessons about `switch`.

Oh, Sorry about that I just tried it the problem was I thought u meant to put the “randomNumber” in all of the switch (all of the cases) my mistake for interpreting it wrong. Also if could could tell me why when I run it it says
`Hello, Kyle! Am i good at nitrotype?: Am i good at nitrotype? The eight ball answered: It is decidedly so`
And that will be all that I need I think i just have to take out the
`console.log(`Am i good at nitrotype?: \${userQuestion}`);`
But I may be wrong.

Other than that thank you for your help all day I have been trying to fix this throughout my classrooms and nobody I asked knew and there was a video on how to do it but my school does has blocked it and wont let me watch it. Otherwise I would’ve just watched it instead of wasting your time. Thank you

1 Like

If you learnt something or at least if my comments were slightly helpful then it wasn’t a waste of time

You are right, this part of output:

``````Am i good at nitrotype?: Am i good at nitrotype?
``````

is the result of this line of code:

``````console.log(`Am i good at nitrotype?: \${userQuestion}`);
``````

This is the code after the changes proposed in my first comment:

``````var userName = 'Jane';

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

var userQuestion = 'Am i good at nitrotype?';

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

\${eightBall}`);
``````
1 Like

Hello everyone! While playing with the code I came up for this exercise, I found out the console is printing `undefined` after my greeting. Does anyone know why this is happening? Attached is my code:

``````let userName = 'Joe';
} else {
console.log('Hello!')
}
};
let userQuestion = 'Will I win the lottery this week?';
console.log( userName + ' wants to ask a question : ' + 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';
break;
};
console.log('The eight ball answered: ' + eightBall);
``````

In this statement you call the `greeting` function. The function prints the greeting as specified inside of its code body. Since you don’t have a `return` statement in the function, the function implicitly returns `'undefined'` to the caller. In this case the caller is `console.log()`, so `'undefined'` gets logged to the console after your greeting. To fix this behavior you can do one of two things. Either change your `console.log()` statements inside your function to `return` the greeting, or just invoke the function without the `console.log()` like so: `greeting(userName)`.

``````let userName = 'Joe';
console.log('Hello '+ userName + '!'); //change console.log to: return
} else {
console.log('Hello!') //same thing here
}
};
``````

Thanks! I just fixed it

1 Like

Hello! Struggled through this and got this as my answer but the code won’t run. Can anyone tell me where i’ve gone wrong?

``````let userName = 'Jess';

let userQuestion = 'Will I ever learn JavaScript?';

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

let eightBall ='';

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

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

This is the only thing preventing your code from running. The else code block executes as a default if no prior if or else if conditions are truthy. You cannot test a condition in your else block. If randomNumber != to 0 through 6 the only other possible value is 7. Happy Coding!

1 Like

Hi there! i managed to make the control flow work with the switch statements but when I use the if / else if the eight ball answer comes before the string:

Hello Martina!
Cannot predict now

Here’s my code:

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

let eightBall = '';

if (randomNumber === 0) {
console.log('It is certain');
}
else if (randomNumber === 1) {
console.log('It is decidedly so');
}
else if (randomNumber === 2) {
}
else if (randomNumber === 3) {
console.log('Cannot predict now');
}
else if (randomNumber === 4) {
console.log('Do not count on it');
}
else if (randomNumber === 5) {
console.log('My sources say no');
}
else if (randomNumber === 6) {
console.log('Outlook not so good');
}
else if (randomNumber === 7) {
console.log('Signs point to yes');

}

``````

Not sure what I’m doing wrong??

In your code, you declaring `eightBall` as an empty string. When you determine what the answer will be with your if…else if statements, you are logging the answer to the console. This happens before your

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

statement. When this statement finally executes, `eightBall` is still empty. If you change `console.log('answer');` to `eightBall = 'answer';` you will get the outcome you were expecting.
Also, just a side note, your final `else if` only needs to be an `else` with no condition to test. If random number wasn’t 0 - 6, 7 is the only value remaining that it could be, so `else` will suffice.
Happy coding!

1 Like

This worked for me on Step 9.

• If you started with a switch statement, convert it to if/else if/else statements. -

I tried over and over again and finally used the ==.
Here’s my code below!

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

const userQuestion = ‘Will I become a mermaid this summer?’;
console.log(`The user asked: \${userQuestion}`);
const randomNumber = Math.floor(Math.random() * 8);
let eightBall = ‘’;

if (randomNumber == 0) {
console.log(‘The eight ball answered: It is certain’);
} else if (randomNumber == 1) {
console.log(‘The eight ball answered: It is decidedly so’);
} else if (randomNumber == 2) {
} else if (randomNumber == 3) {
console.log(‘The eight ball answered: Cannot predict now’);
} else if (randomNumber == 4) {
console.log(‘The eight ball answered: Do not count on it’);
} else if (randomNumber == 5) {
console.log(‘The eight ball answered: My sources say no’);
} else if (randomNumber === 6) {
console.log(‘The eight ball answered: Outlook not so good’);
} else if (randomNumber == 7) {
console.log(‘The eight ball answered: Signs point to yes’);
} else {
console.log(‘The eight ball is broken’)
};

So the code works perfectly without

``````let eightBall = '';
``````

Can you tell me why the exercise called for this in the first place?

What code works perfectly without `let eightBall = '';`? Could you share the code in question? The declaration of the variable `eightBall` has a specific purpose the way it is used in the lesson. If you are using either a `switch ... case` block, or an `if ... else if ... else` block to assign a value to `eightBall` like so; `eightBall = 'It is decidedly so.'`, without the aforementioned variable declaration, you end up declaring `eightBall` as a global variable at the time of assignment. In a small project like this, there is little to no consequence, but it is very bad practice to give any variable scope beyond it’s needs. It can lead to many problems in a large more complicated project. By using `let eightBall = ''` prior to assigning a value later, you are limiting the scope to the block of code in which it was declared including any nested code blocks.

3 Likes

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

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

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

\${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.

1 Like

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
``````

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,

``````let userName = "";

let userQuestion ="Will I be a celebrity?";

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:
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;
}
``````