Magic Eight Ball

Hi,
I’m trying to test if/else if/else statement on Magic Eight Ball exercise. When I run the code, it only shows up the if or the else statement but not else if statements. How can I make the code runs all of the else if statements?

https://www.codecademy.com/paths/build-web-apps-with-react/tracks/bwa-javascript-functions-arrays-and-loops/modules/learn-javascript-control-flow/projects/magic-eight-ball-1

let userName = “Thien Nguyen” ;
userName ? console.log(Hello, ${userName}) : console.log(“Hello!”);
let userQuestion = “Will the sun shine?”;
console.log(The user asked: ${userQuestion});

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

let eightBall = ‘’;

if(randomNumber) {
eightBall = ‘It is certain’;}

else if (randomNumber) {
eightBall = ‘It is decidedly so.’;
}

else if (randomNumber) {
eightBall = ‘It is decidedly so’;
}

else if (randomNumber) {
eightBall = ‘Reply hazy try again’;
}

else if(randomNumber) {
eightBall = ‘Cannot predict now’;
}
else if(randomNumber) {
eightBall = ‘Signs point to yes’;
}
else if(randomNumber) {
eightBall = ‘My sources say no’;
}
else if(randomNumber) {
eightBall = ‘Outlook not so good’;
}

else {
eightBall = ‘Do not count on it’;
}

console.log(eightBall);

What is different about this condition from the if condition? Or any of the subsequent else if conditions?

1 Like

Wow!, I just let randomNumber == 0 to 7 and it works, but it won’t execute the last else statement when I run the code several times. Is there anything wrong with the else statement?

let userName = “Thien Nguyen” ;
userName ? console.log(Hello, ${userName}) : console.log(“Hello!”);
let userQuestion = “Will the sun shine?”;
console.log(The user asked: ${userQuestion});

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

let eightBall = ‘’;

if(randomNumber == 0) {
eightBall = ‘It is certain’;}

else if (randomNumber == 1) {
eightBall = ‘It is decidedly so.’;
}

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 = ‘Signs point to yes’;
}
else if(randomNumber == 6) {
eightBall = ‘My sources say no’;
}
else if(randomNumber == 7) {
eightBall = ‘Outlook not so good’;
}

else {
eightBall = ‘Do not count on it’;
}

console.log(eightBall);

Are there nine possible outcomes? Or eight? Give it some thought…

Since the program dictates the eight possible outcomes, there is no need for a default (else).


Extra Study

Consider,

x = Math.floor(Math.random() * 8)

What does it return?

{x | 0 <= x < 8}

In other words, a digit, one of {0, 1, 2, 3, 4, 5, 6, 7}.

Interesting how closely this resembles the index of an array. What if our data was stored in an array rather than expressed in literals in a control flow structure (if)?

outcomes = [
  'It is decidedly so',
  'Chances are good',
  'Reply hazy try again',
  'Cannot predict now',
  'Signs point to yes',
  'My sources say no',
  'Outlook not so good',
  'Do not count on it'
]
eightball = outcomes[x]
console.log(eightball)

Perhaps not the focus of this lesson, but an observation and exploration. Data structures can really help in some cases to limit or remove logic.


Chrome console

 > magicEightBall = () => outcomes[Math.floor(Math.random() * 8)]
<- () => outcomes[Math.floor(Math.random() * 8)]
 > magicEightBall()
<- "My sources say no"
 > magicEightBall()
<- "Signs point to yes"
 > magicEightBall()
<- "Outlook not so good"
 > 

Thanks Roy for showing me how to use condition and array. I appreciated.

1 Like