Build a Fortune Teller

Hi,
I am having trouble with the selectedFortune portion. I checked MDN and other forum posts and I’m still doing something wrong.
Before adding the “let selectedFortune” line of code, my previous code passed. After adding it and the following code, it’s causing everything to fail.
My current code is below.
Thank you for your help. :slightly_smiling_face:

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 randomNum = Math.floor(Math.random() * 5) + 1;

console.log(randomNum);

let selectedFortune = "";

 { if (randomNum === 1)
    (selectedFortune = fortune1);
   if 
     (randomNum == 2)
      (selectedFortune = fortune2);
    if 
     (randomNum == 3)
      (selectedFortune = fortune3);
   if 
     (randomNum == 4)          
      (selectedFortune = fortune4);
   if 
    (randomNum == 5)   
                selectedFortune = fortune5;

  return result;}

console.log(selectedFortune);

I dont understand it passes but it doesnt why dont you just use the code that passes?

1 Like

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

It is also useful to include a direct link to the challenge for context, so I have added that also.

1 Like

You have some issues with your code syntax and formatting:

let selectedFortune = "";

 { if (randomNum === 1)
    (selectedFortune = fortune1);
   if 
     (randomNum == 2)
      (selectedFortune = fortune2);
    if 
     (randomNum == 3)
      (selectedFortune = fortune3);
   if 
     (randomNum == 4)          
      (selectedFortune = fortune4);
   if 
    (randomNum == 5)   
                selectedFortune = fortune5;

  return result;}

The correct syntax and correct formatting for an if statement is:

if (condition) {
  // code to execute if condition is true
}

You have enclosed all of your if statements within a pair of curly brackets, which is also not correct syntax. You also have a return statement (with an undefined variable), which cannot be used outside of a function.

The simplest way to fix these issues would be to refactor and reformat your code as a series of if (or better still if ... else if) statements and remove the faulty syntax.

Finally, you are not precisely fulfilling this User Story:

You should select a random number between 1 and 5, inclusive, and assign it to the variable randomNumber.

More info on conditional statements:

1 Like

Hey! Thank you for your help.

This is what I came up with, but no luck on passing.
I’m able to generate a random number, but I’m still missing the assign it to the variable part. And selectedFortune isn’t working.

var randomNum = Math.floor(Math.random() * 5) + 1;
 
console.log(randomNum);

var selectedFortune = randomNum;
if (ramdomNum == 1) {selectedFortune = fortune1;
} else if (randomNum == 2) {selectedFortune = fortune2;
} else if (randomNum == 3) {selectedFortune = fortune3;
} else if (randomNum == 4) {selectedFortune = fortune4;
} else (randomNum5 == 5) {selectedFortune = fortune5;
}

console.log(selectedFortune);

Check your console, there should be an error message.

You cannot code just by looking at the tests. Don’t worry about the tests yet, first you need to write a working program. It will be working when you get the results that you want in the console.

When it’s all working as it should, then look and see if it passes the tests.

1 Like

This is the first part of my code. If I have just this in the lab and run the tests, I pass the first 6 items and generate a new number each time I submit the code.
If I add the code I wrote for the selectedFortune part, it makes everything fail; even what previously passed through step 6.

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.";


var randomNum = Math.floor(Math.random() * 5) + 1;
 
console.log(randomNum);

Read what I wrote above about the tests (I edited that a few times so it may be different now)

Check your console, there should be an error message.

If it’s not open you might need to click the “Console” button in the top right

1 Like

I don’t know whether the problem you are having is with your last “else clause”. That else clause is testing a condition which shouldn’t be. Why don’t you correct that first. Either add an if to the else clause or remove the condition completely and see where it takes you.

1 Like

This is my console output. Each time I run the code, it populates a random number with the correct fortune. I’m still missing step 7 & 9 :melting_face:

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.";


var randomNum = Math.floor(Math.random() * 5) + 1;

console.log(randomNum);

var selectedFortune = randomNum;
if (randomNum == 1) {selectedFortune = fortune1;
} else if (randomNum == 2)  {selectedFortune = fortune2;
} else if (randomNum == 3)  {selectedFortune = fortune3;
} else if (randomNum == 4)  {selectedFortune = fortune4;
} else if (randomNum == 5) {selectedFortune = fortune5;
}

console.log(selectedFortune);










Here are some troubleshooting steps you can follow. Focus on one test at a time:

  • What is the requirement of the first failing test?
  • Check the related User Story and ensure it’s followed precisely.
  • What line of code implements this?

If this does not help you solve the problem, please reply with answers to these questions.

HINT: You need to use the same variable names described in the instructions, you can’t change them

1 Like

:skull: Wowwwww. I can’t believe I missed that. :skull:
(sigh) calling it a day.
Thank you to everyone who took the time to help me on this :slightly_smiling_face:

1 Like

It’s often some little thing like that, don’t sweat that.

What you should sweat is the formatting of your if statements. You can review @igorgetmeabrain comment about this.

Formatting your code correctly helps:

  • you read your code clearly and see mistakes
  • helps other people read your code easily
2 Likes