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:
- You should initialize the five variables
fortune1
,fortune2
,fortune3
,fortune4
, andfortune5
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."
- You should select a random number between 1 and 5, inclusive, and assign it to the variable
randomNumber
. - You should create a
selectedFortune
variable and assign the appropriate fortune based on these rules:
- If
randomNumber
is 1, assign the value offortune1
toselectedFortune
. - If
randomNumber
is 2, assign the value offortune2
toselectedFortune
. - If
randomNumber
is 3, assign the value offortune3
toselectedFortune
. - If
randomNumber
is 4, assign the value offortune4
toselectedFortune
. - If
randomNumber
is 5, assign the value offortune5
toselectedFortune
.
- 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