Help with Magic Ball SWITCH mode

I did this for the practice, however, the output is this one:
Hello, Paulina
Paulina asked: Will I get a job?
The eight ball answered:

And then nothing happens. It’s like it’s ignoring the whole cases. What am I doing wrong?
Thank you in advance for your help.
Paulina

let userName = "Paulina";
userName ? console.log(`Hello, ${userName}`) : console.log("Hello!");

const userQuestion = "Will I get a job?";
console.log(`${userName} asked: ${userQuestion}`);

const 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 = "Replay hazy, tray 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 = "Absolutly YES!";
    break;
  case 7:
    eightBall = "Signs point to yes";
    break;
}
console.log("The eight ball answered: " +(eightBall));

Check how you’re invoking Math.random(). Right now it will not return a valid result (which is why your switch isn’t working).

MDN documentation almost always has useful examples, including in this case: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

1 Like

OMG! it was just a small parenthesis- It drove me nuts the whole afternoon and through the night! thank you so much!

I was too happy, too soon. I added the parenthesis, but only retrieves the first case, case 0, and not randomizing the rest.

assuming you did this:

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

i see the problem. You first generate a random number between 0 and 1, which you then round down/floor. Which will be zero, which you multiply by 8. zero times eight will always be zero

I understand that, but the exercise says " We need to generate a random number between 0 and 7.

Create another variable, and name it randomNumber. Set it equal to this expression, which uses two methods from the Math library."

but if that is the plan, shouldn’t you first generate the desired random number before rounding down?

could be something like let randomNumber = Math.random() * 8;
randomNumber = Math.floor()

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

nevermind! got it! You are a genius. At first, I needed a parenthesis. Now I needed even more! Lol. TY so much! I’ve been trying since 6.AM! lol

1 Like

you could even do:

Math.floor(Math.random() * 8);

which is easier to read

the important thing is to multiple first before rounding down. Otherwise, the value between 0 and 1 generated by random gets floored to zero right away, which won’t do you much good

of course you can even do this in two steps:

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

flooring the random number and updating the variable.

2 Likes

Thank you! on to the next exercise! I’m so happy! Lol.

1 Like