Magic 8 Ball - Always the Same answer

I tried looking for this answer but can’t find it. I am doing the Magic 8 Ball lesson. In step 9 it states that I should run my program a few times to see random results appear. Whenever I run the program I repeatedly get “It is decidedly so” It is almost like the code isn’t multiplying by 8 when inside Math.floor for me. I have included my code from the lesson below.

//Enter your username in the quotes
let userName= 'Carrie';
userName ? console.log(`Hello ${userName}!`) : console.log('Hello!');
//Ask your question in the quotes
let userQuestion = 'Will I master code?';
console.log(`${userName}, you have asked me: "${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 = 'Relpy hazy try again';
} else if (randomNumber = 3) {
  eightBall = 'Cannot predict now';
} else if (randomNumber = 4) {
  eightBall = 'Don\'t count on it';
} else if (randomNumber = 5) {
  eightBall = 'My sources say no';
} else if (randomNumber = 6) {
  eightBall = 'Outlook not so good';
} else if (randomNumber = 8) {
  eightBall = 'Signs point to yes';
}
console.log(eightBall);

I also did some additional testing in the Google Chrome interpreter and it won’t deviate numbers and seems to forget to multiply when inside Math.floor. You can see my results below:

let myNumber= Math.random() * 8;
console.log(myNumber);
console.log("Also my " + myNumber);
VM606:2 7.397933413740189
VM606:3 Also my 7.397933413740189
undefined
let myNumber= Math.floor(Math.random() * 8);
console.log("My new number:" + myNumber);
VM910:2 My new number:1
undefined
console.log("Another number" + myNumber);
VM1087:1 Another number1
undefined
let newNumber = Math.random() * 8;
console.log(newNumber);
VM1433:2 7.302228163706628
undefined
console.log(newNumber);
VM1509:1 7.302228163706628
undefined

Any help or explanation would be appreciated and I 100% accept that maybe I am messing up, I’m just not seeing where.

Hi Mishael,
welcome to the forums!
The problem with your code is that you don’t check if a condition is met but you always assign a new value to randomNumber in your if and if else clauses.

I thinks it’s because you’re assigning the value (one equal sign) instead of comparing it. Remember that = assigns a value, == compares values to left and right regardless of type and === is stricty equality (value and data type).
So in line 9 (if I counted corrected) your IF statement is not comparing the variable randomNumber to the number zero, I think it should be if (randomNumber == 0) or if (randomNumber === 0)

That was exactly what I was missing! Things are working well now! I completely forgot about reading the differences between =, ==, and ===! All part of the learning process I suppose.

1 Like

I’m having this same issue as well but the issue doesn’t seem to be the equal sign… can anyone see what i’ve done wrong?

let username = ‘’;

if (username) {
console.log('Hello ’ + username + ‘!’);
} else {
console.log(‘Hello!’)
};

let userQuestion = ‘Will things go my way?’

console.log(userQuestion);

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

let eightBall = ‘’;

if(randomNumber == 0){
eightBall = ‘It is certain’;
} else if (randomNumber == 1){
eightball = ‘It is decidely 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)

‘else’ doesn’t accept a condition. It’s either else {} or else if( condition ) {}.
If your problem still persists after you fixed that, please

  1. open a new topic
  2. post a link to the lesson
  3. describe what result you expect and what you get instead
  4. format your code following this guideline
  5. post the error message if there is any

thank you! That was the issue

1 Like