Can someone roast my code please?

Hello. I’m having trouble with this Magic8Ball task: https://www.codecademy.com/courses/introduction-to-javascript/projects/magic-eight-ball-1
It seems it won’t randomise the number. It just keep returning number 1 (It Is decidedly so)
What have I done wrong?

Thanks.

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

let userQuestion = "Will I die?";
console.log(`${userName} just asked ${userQuestion}`)

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 {
    eightBall = 'Signs point to yes';
}

console.log(`Magic 8 Ball says: ${eightBall}`)
1 Like

if that is your suspicion, simple enough to find out right:

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

run this code a couple of times, see if you get random numbers. Do you? If so, that generating the random number is not the problem

so what could be the problem? It seems to go wrong here:

 else if (randomNumber = 1) 

what could it be?

2 Likes

I agree, there’s a simpler way to accomplish the main goal than the “else if” method. In a case like this, it’s better to break from the default trains of thought, otherwise you’re going to end up on the wrong side of the Eight Ball a lot more often… :innocent:

2 Likes

you must write the code as follows :
if (randomNumber === 0) …
else if (randomNumber === 1) …

You need to use 3 equal signs in the if/else statements. By using the === operator, your getting a boolean true or false back (which then either runs the eightBall code or checks the next else/if statement).

By using just the 1 equal sign, you are essentially declaring randomNumber equal to 0 in the first if statement.

1 Like

I should have clarified; I used the Switch method before using the else/if method (as it suggests at the end of the task) and it worked fine with Switch method. But not with else/if. Anyway, I’m past it now.
Thanks.