Problems with Magic Eight Ball exercise

Hello, everyone.

I’ve been practicing Java Script in the Intro course and i have some problems with the Magic Eight Ball exercise. I have two questions.

I made first with switch and second with if-else, in the switch it ran fine, but when i code in if-else structure i only get one result, which is ‘It is certain’, as if my randomNumber would only be 1. Why is it running like that? Because it’s running fine with switch i guess my problem is in constructing if-else, but i can’t find anything wrong in it.

Also, my switch never runs the default block, why is that? I think that my default is set to zero, Math.random() doens’t select number zero?

Here is my code:

let userName='Victor';
userName? console.log(`Hello, ${userName}!`):console.log(`Hello!`);
let userQuestion='Will it rain tommorow?';
console.log(userQuestion);
let randomNumber=Math.floor(Math.random()*8);
let eightBall='';

if (randomNumber=0){
   eightBall='Signs point to yes';
}else if (randomNumber=1){
  eightBall='It is certain';
}else if (randomNumber=2){
  eightBall='It is decidedly so';
}else if(randomNumber=3){
  eightBall='Reply hazy try again';
}else if(randomNumber=4){
  eightBall='Cannot predict now';
}else if (randomNumber=5){
  eightBall='Do not count on it';
}else if (randomNumber=6){
  eightBall='My sources say no';
}else if (randomNumber=7){
  eightBall='Outlook not so good';
}else {
  eightBall='Who knows, man, i\'m just a ball';
}
/*
switch (randomNumber) {
  case 1:
    eightBall='It is certain';
    break;
  case 2:
    eightBall='It is decidedly so';
    break;
  case 3:
    eightBall='Reply hazy try again';
    break;
  case 4:
    eightBall='Cannot predict now';
    break;
  case 5:
    eightBall='Do not count on it';
    break;
  case 6:
    eightBall='My sources say no';
    break;
  case 7:
    eightBall='Outlook not so good';
    break;
  case 8:
    eightBall='Who knows, man, im just a ball';
    break;
  default:
    eightBall='Signs point to yes';
    break;
}
*/
console.log(`The Eight Ball speaks: ${eightBall}`);

Thank you for your time!

You need to use a comparison operator in your if statements.
Your default block should be running as your randomNumber variable will be from 0-7. Are you sure it’s not case 8: that’s not running. Try logging randomNumber right after you create it and run it a few times to check.

1 Like

So, i shoud actually use === instead of =, that is the problem with my if-else structure?

Why would my random number go from 0 to 7? I multiply it by 8, shoudnt it go from 0-8?
But if it only goes up to 7 i believe my default doesnt run because the code just never gets to it, as it probably happens with the 8 too and i failed to notice, it always stops on some statment earlier.

Yes. ‘===’, ‘==’, ‘=’ are all different. https://www.w3schools.com/js/js_operators.asp

Math.random() gives you a number between 0 and 1, including 0 but not 1 itself. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

2 Likes

Nice, thank you very much for your help!