Build a Fortune Teller - Build a Fortune Teller

Tell us what’s happening:

I get a fail in every Task except 6. I dont understand the problem

Your code so far

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0

Challenge Information:

Build a Fortune Teller - Build a Fortune Teller

There are few changes to do.

Instead of directly assigning selectedFortune to randomNumber
Just declare the variable and don’t assign anything to it… you actually forgot to declare the variable selectedFortune in your code.

 let selectedFortune;

and after that you might wanna change the if blocks as they are not passing the tests
instead of logging in the if block we can use the selectFortune variable after the if-else block and log it afterwards and now as random number and selectedFortune are not equal you might want to change the condition used in the if blocks

The structure would end up something like this:

if (randomNumber == ????) {
  whatever goes here
} else if (randomNumber == ????) {
  whatever goes here
} else if (randomNumber ==  ????) {
  whatever goes here
} else if (randomNumber ==  ????) {
  whatever goes here
} else if (randomNumber ==  ????) {
  whatever goes here
}

console.log(selectedFortune);

Hope this helps :sparkles:

1 Like

Hi.

Look at your Math.random calc again. Have a look online how to do this, there are a lot of examples out there.

You have not used the let keyword to declare your selectedFortune variable.

You have not done story 4 - log selectedFortune to the console.

The console needs to display the value of selectedFortune. The execution statements inside your code don’t assign any values to selectedFortune at present. Tweak your executions statements so that the right fortune is assigned to the variable selectedFortune depending on the conditions in each if statement.

No need for a default else statement here, all have a different condition.

You don’t need to change your variable names if you make these tweaks. I made your code work with the conditions as you have written them. @xoAURAxo has suggested an alternative way to write the code but you can tweak your own code as well to make it work.

2 Likes

Ah @a1legalfreelance I see… I thought out of the box to debug it LOL.. Thanks I will take notes on this.

1 Like

No worries - I did mine the way you did it as well but actually it can work how the OP has done it!

1 Like