Build a Fortune Teller - Build a Fortune Teller

Tell us what’s happening:

I have all of the correct answers except for #3. I assume I need to do some kind of “if else” statement to ensure that the randomNumber variable is assigned to the corresponding fortune# value but I am not 100% sure that is correct.

Also, I ended it with an “else if” statement but I think it should have ended with just “else” but the instructions are vague enough that I am not sure the “if else” were necessary as a whole. If I don’t use “if else”, I have no idea what else to put in its place.

Listed below are the instructions to build a fortune teller:

User Stories:

  1. You should initialize the five variables fortune1, fortune2, fortune3, fortune4, and fortune5 with a string value of your choice. You can use the below options if you like:
  • "Your cat will look very cuddly today."
  • "The weather will be nice tomorrow."
  • "Be cautious of your new neighbours."
  • "You will find a new hobby soon."
  • "It would be wise to avoid the colour red today."
  1. You should select a random number between 1 and 5, inclusive, and assign it to the variable randomNumber.
  2. You should create a selectedFortune variable and assign the appropriate fortune based on these rules:
  • If randomNumber is 1, assign the value of fortune1 to selectedFortune.
  • If randomNumber is 2, assign the value of fortune2 to selectedFortune.
  • If randomNumber is 3, assign the value of fortune3 to selectedFortune.
  • If randomNumber is 4, assign the value of fortune4 to selectedFortune.
  • If randomNumber is 5, assign the value of fortune5 to selectedFortune.
  1. You should log the selectedFortune to the console.

Your code so far

let fortune1 = "Your cat will look very cuddly today.";
let fortune2 = "The weather will be nice tomorrow."
let fortune3 = "Be cautious of your new neighbors.";
let fortune4 = "You will find a new hobby soon.";
let fortune5 = "It would be wise to avoid the color red today.";
let randomNumber = Math.floor(Math.random() * 5) + 1;
let selectedFortune = randomNumber;
if (randomNumber == 1) {
  console.log(fortune1);
} else if (randomNumber == 2) {
  console.log(fortune2);
} else if (randomNumber == 3) {
  console.log(fortune3);
} else if (randomNumber == 4) {
  console.log(fortune4);
} else if (randomNumber == 5) {
  console.log(fortune5);
}

console.log(selectedFortune);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Build a Fortune Teller - Build a Fortune Teller

@calypsocodes You aren’t assigned fortune’s to the variable.

I thought I did that with:

let selectedFortune = randomNumber;

just above the if else statements. Is that the part that is incorrect? Are the if else statements okay?

If randomNumber is 1, assign the value of fortune1 to selectedFortune.

Did you assign the value of fortune1 to selectedFortune?

let selectedFortune = randomNumber;

You are assigning a random number to selectedFortune.

Nope.

You should log the selectedFortune to the console.

What are you logging to the console?

I thought I did that too by including the if/else statements after. And I did console log selectedFortune at the end.

I don’t understand what I am doing wrong. What part of the code are you saying is specifically incorrect?

Focus on one thing at a time then:

If randomNumber is 1, assign the value of fortune1 to selectedFortune.

What are you assigning to selectedFortune ?

let selectedFortune = randomNumber;

Thank you for trying to help, but I still didn’t understand. Someone showed me how to use AI and I was able to learn what I did wrong that way. Thanks!

Are you able to explain to me what you did wrong?

Yes. I was assigning randomNumber directly to selectedFortune instead of assigning it to the appropriate fortune string.

So while I was on the right track with everything else, I just messed up the console.log sections in each if-else statement in my original coding listed above. I made the adjustment, and it passed.

1 Like