Javascript Magic 8 ball question

I finished the magic 8 ball project using the switch method but wanted to try the “if else” method too. I don’t get an answer when I run it but I think its because my if else statements aren’t linked to the randomNumber. Does anyone have any tips ?

1 Like

Is it because randomNumber was never called? For example, eightball is equal to nothing right now because on line six you defined it as ‘’. So what if instead of that, you found some way to make randomNumber be linked to the eightBall variable? Because what your code does now is reply null because eightball is linked to nothing.

1 Like

Line 6 is a deal breaker.

1 Like

I think you intended to have if (eightball === 0) actually be: if (randomNumber === 0) and so on in all of your if statements. Instead of logging the response to the console, if you use eightBall = 'response' inside each of your if(condition) code blocks, your output on line 24 should give you the desired result.

2 Likes

Exactly, or if @rrmatt wants to keep the eightBall variable, define eightball as randomNumber

1 Like

True, but for the desired outcome, he would then need to prepend his responses with "The eightball answered: " , and delete line 24. Otherwise the output would be for example:
Hello, Ryan!
Ryan asked: Will I make an A on my math test tomorrow?
Signs point to yes
The eightball answered: 7

Oooh you’re right.
Of course you could make eightBall equal to all of the strings instead of logging them to the console immediately.

Was this ever solved? I had the same problem. + more.

  1. I don’t understand why line 6 is wrong, my code is the same and it is the same as the project.
  2. Why are we using === in eightBall for the if/else statement when it was just = with switch?
  3. Why are we using eightBall in the code instead of randomNumber?
  4. Why do i get errors saying Uncaught SyntaxError: Unexpected token ‘=’
    Is it because we are using === instead of =?

Can someone help ? Simple explanation?

let userName = '';

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

const userQuestion = 'Will I ever understand JavaScript?';

console.log(`${userName} asked: ${userQuestion}`);

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

let eightBall = '';

if(eigthBall ==== 0){console.log('It is Certain');}
else if(eightBall ==== 1){console.log('I think so');}
else if(eightBall ==== 2){console.log('Looks Hazy try again');}
else if(eightBall ==== 3){console.log('Cannont predict now');}
else if(eightBall ==== 4){console.log('Do not count on it');}
else if(eightBall ==== 5){console.log('my Sources say no');}
else if(eightBall ==== 6){console.log('Outlook not so good');}
else if(eightBall ==== 6){console.log('Yes');}

console.log(eightBall);```

There is nothing to compare. That variable could/should be set with the random number.

You’ve got two sixes, btw.

Thank you for the reply. I noticed i was using ==== instead of === (not sure why we used = before?) so i fixed that.

when I set eightBall = randomNumber; now it just spits out random numbers…kind of…

let userName = 'Brad';

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

const userQuestion = 'Will I ever understand JavaScript?';

console.log(`${userName} asked: ${userQuestion}`);

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

let eightBall = randomNumber;

if(randomNumber === 0){console.log('It is Certain');}
else if(randomNumber === 1){console.log('I think so');}
else if(randomNumber === 2){console.log('Looks Hazy try again');}
else if(randomNumber === 3){console.log('Cannont 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('Yes');}
else{console.log('This is not going well');}
console.log(eightBall);```

which is what that line does.

Outlook not so good
7

I don’t get it…I only get numbers in the response. I thought eightBall = randomNumber which is set to different things in the if/else statements? I don’t get any text in the response.

Hello Brad!
Brad asked: Will I ever understand JavaScript?
Cannont predict now
3

I was able to get this response after re sizing the window and the view of the console changed. Thank you for your help.

Still not sure why there had to be all the changes outside the if/else statement when we were just changing the switch statement itself? I don’t remember reading about this in the lessons, but I seem to forget a lot of things about javaScript.

That’s not a switch but and if…else if…else.

We are in charge of what our program does. If you plan it out with a pencil and paper, sketch what steps will take place and in what order. You can find ways to improve your code if you don’t just set it aside and forget it. Come back to it in a week or two and go through it again.

Pencil and paper are helpful in planning but it seems like JavaScript works backwards so never really know how to write the code or what other code is saying. Maybe in a week or two I can review this course (again) and try to understand.

1 Like