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 trial
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsConditional Challenge.
Hi guys. It took me 3 days but I finally did it. I wanted to complete the @Guil Hernandez conditional challenge before looking at The solution. The challenge was to build a program that
- Asked at least five questions 2.Kept track of the number of questions the user answered correctly. 3.Provided a final message after the quiz letting the user know the number of questions he or she got right.
- Ranked the player. If the player answered all five correctly, give the player a gold crown; 3-4 a silver crown; 1-2 a bronze crown and 0 correct is no crown.
It took 3 days but I got it and I'm so excited to share and get your feedback. How would you have done it? here is the program. I used a bunch of the stuff we covered in the first courses. Thanks guys.
var questions = 5;
var questionsLeft = ' [' + questions + ' questions left]';
var question1 = prompt('What is the capital of China?' + questionsLeft);
questions -= 1;
var questionsLeft = ' [' + questions + ' questions left]';
var question2 = prompt('What color is the sky?' + questionsLeft);
questions -= 1;
var questionsLeft = ' [' + questions + ' questions left]';
var question3 = prompt('What is the shape of a ball?' + questionsLeft);
questions -= 1;
var questionsLeft = ' [' + questions + ' questions left]';
var question4 = prompt('What is the first letter of the alphabet' + questionsLeft);
questions -= 1;
var questionsLeft = ' [' + questions + ' questions left]';
var question5 = prompt('What is the last letter of the alphabet' + questionsLeft);
questions -= 1;
var correctAnswers = 0;
if ( question1 === 'Beijing' || question1 === 'beijing' || question1 === 'BEIJING' || question1 === 'Peking' || question1 === 'peking' || question1 === 'PEKING') {
correctAnswers += 1;
} if ( question2 === 'Blue' || question2 === 'blue' || question2 === 'BLUE' ) {
correctAnswers +=1;
}
if ( question3 === 'Round' || question3 === 'round' || question3 === 'round' || question3 === 'Circle' || question3 === 'circle' || question3 === 'CIRCLE' ) {
correctAnswers +=1;
}
if ( question4 === 'A' || question4 === 'a' ) {
correctAnswers +=1;
}
if ( question5 === 'Z' || question5 === 'z' ) {
correctAnswers +=1;
} else {
correctAnswers;
} if ( correctAnswers === 5) {
document.write('you answered all 5 questions correctly. you win a Gold crown!');
} else if ( correctAnswers === 4) {
document.write('you answered 4 questions correctly. you win a silver crown!');
} else if ( correctAnswers === 3) {
document.write('you answered 3 questions correctly. you win a silver crown!');
} else if ( correctAnswers === 2) {
document.write('you answered 2 questions correctly. you win a bronze crown!');
} else if ( correctAnswers === 1) {
document.write('you answered 1 question correctly. you win a bronze crown!');
} else {
document.write('Sorry. You did\'t answer any question correctly!.');
}
1 Answer
Luc de Brouwer
Full Stack JavaScript Techdegree Student 17,939 PointsWell done! You could tweek your code a little bit, for example
correctAnswers +=1;
// Could be
correctAnswers++;
// Same effect, less code
You could also make an array that holds the questions and use the index to keep track which questions are asked instead of defining every question in a variable. For identical strings, you could also make a function that automaticly puts the answers to lowerCase for you, so you don't have to compare more values than one
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsThank you Luc de Brouwer Thanks for the suggestions. Since I haven't gotten to that level yet, I'm still just in the beginning of the course, we haven't touched on these methods yet. Right now we are doing, scope. But once we get there and I understand the concept I will try to rewrite it using what you suggested. I'm trying to build on the challenges as I learn a new concept. I also like that Guil Hernandez uses the same method where he adds a new concept to a challenge he gave previously. Thanks for the reply. Please continue to reply. I'm sure I will have more moments where I get stuck.
SAMUEL LAWRENCE
Courses Plus Student 8,447 PointsSAMUEL LAWRENCE
Courses Plus Student 8,447 PointsHi Luc de Brouwer some time ago you gave me a suggestion to make an array that holds the the questions and use the index to keep track of which questions re asked. I've completed the JavaScript Loops, array and objects course and decided to revisit this . While I now understand what you're saying I still have some difficulty using the index in arrays, it's still a bit confusing to me.
I'm also practicing generating random things, I've seen how to generate a random number and a random color. I want to extend this and generate a random day. Steven Parker told me once I could create an array of the days of the week and something something, but for some reason I can't find the post where he responded. It would be really helpful if you could show me an example of how you would do that?
Thanks.
I'm practicing so this stays fresh in my head.
Steven Parker
243,318 PointsSteven Parker
243,318 PointsStart a fresh question and describe what you're trying to do in a bit more detail.