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.
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’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.
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.
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.
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);
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.
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);