Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJim McQuarrie
10,597 Pointsadding an if else statement
Thought I would make this one a little more interesting by adding an if else statement for gender selection with a little different twist! This is mostly so I can extend my learning and maybe expand how I might solve problems. When I started to review this section I wanted to make it different and I quickly realized that one would need to specify a gender!
var gender = prompt("please answer 1 for if your childs a boy or 2 if your childs a girl");
var playerName = prompt("please tell us your son's or daughters name!");
var playerSport = prompt("Please tell us your son's or daughters sport!");
var excelsAt = prompt("Please tell us another thing your child excels at!");
var student = prompt("what kind of student is your child?");
alert("You are finished inputing your answers, are you ready for the answer?");
if (gender === "1") {
gender = "son";
} else {
gender = "daughter";
}
var message = "My " + gender + " , " + playerName;
var message2 = message + " is a great " + playerSport + " player and also excels at ";
var message3 = message2 + excelsAt + ". My " + gender + " makes us very proud by being a ";
var message4 = message3 + student + " student in school";
document.write( message4);
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsI'm not sure if you're asking for an alternative syntax or not, but the code you've written so far looks great! :)
You could specify another number for the female gender like so.
if (gender === "1") {
gender = "son";
} else if(gender ==="2") {
gender = "daughter";
}
else {
alert("Please try again!");
}
So you use else if to start another code block for the second gender and then close out with a final else statement. Good luck. :)
Chris Ward
12,129 PointsThis isn't a JavaScript issue. It's better solved by using an HTML form. Too many prompts makes Scrooge extra grouchy!
Jim McQuarrie
10,597 PointsJim McQuarrie
10,597 PointsJust playing around and trying to alter the original challenges so I can learn more :)