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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Zankruti Murray
Zankruti Murray
8,969 Points

My Solution. Would really appreciate some comments/feedback. Thank you

var questions = 5;
var answer = 0;
question = prompt('ONE: Which metal is heavier, silver or gold?');
if (question.toLowerCase() === 'gold'){
    document.write('<h2> 1. Gold is the correct answer.</h2> <br>');
    answer += 1;
} else {
document.write('<h2>1. Sorry! That answer is incorrect.</h2><br>');
}
question = prompt('TWO: How many legs do butterflies have?');
if (question.toLowerCase() === 'six' || question === '6'){
    document.write('<h2> 2. Six is the correct answer.</h2> <br>');
    answer += 1;
} else {
document.write('<h2>2. Sorry! That answer is incorrect.</h2><br>');
}
question = prompt('THREE: What is the largest mammal in the world?');
if (question.toLowerCase() === 'blue whale'){
document.write('<h2> 3. Blue Whale is the correct answer.</h2><br> ');
answer += 1;
} else {
document.write('<h2>3. Sorry! That answer is incorrect.</h2><br>');
}
question = prompt('FOUR: What is the rhino\'s horn made of?');
if (question.toLowerCase() === 'hair'){
document.write('<h2> 4. Hair is the correct answer.</h2> <br>');
answer += 1;
} else {
document.write('<h2>4. Sorry! That answer is incorrect.</h2><br>');
}
question = prompt('FIVE: What is the tallest animal in the world?');
if (question.toLowerCase() === 'giraffe'){
document.write('<h2> 5. Giraffe is the correct answer.</h2> <br>');
answer += 1;
} else {
document.write('<br><h2>5. Sorry! That answer is incorrect.</h2>');
}
if (answer > 0){
document.write('<h1> Congratulations!!! You have ' + answer + ' correct answers</h1>');
} else {
 document.write('<h1> Oh!!! You have ' + answer + ' correct answers</h1>');
}
if (answer === 5){
document.write('<h1> Your Rank is Gold Crown. Outstanding!!!.');
} else if(answer === 4 || answer === 3){
document.write('<h1> Your Rank is Silver Crown. Excellent!!!.');
} else if ( answer === 2 || answer === 1) {
document.write('<h1> Your Rank is Bronze Crown. Great!!!.');
} else {
document.write('<h1> Sorry! You have no Crown. Better Luck Next Time!');
}

2 Answers

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

you're doing great, later in this course you will see the solution with use of while loop instead of lot's of if statements. Also when you'll get familiar with functions in javascript you will understand how to keep the logic of displaying information inside the functions and your code will be looking more friendly, but it's absolutely good for now. Keep coding ;-)