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

Emanuel Rouse
Emanuel Rouse
1,985 Points

Here's my Conditional Challenge Quiz Program. Anything I can do to improve it?

https://w.trhou.se/wbihxjeqvm

Just wondering if there is anything I can do up to this point in the learning to make this code more efficient? Any advice would be helpful.

2 Answers

Emmanuel C
Emmanuel C
10,636 Points

Hey we have the same name :D

As for your code, those else statement arent really doing anything, and can be removed. I would also avoid using document.write but instead put those strings into variables then use the innerHtml property to insert it into your html. So you can add another div element inside your container, give it the id of output. Then you can do...

var ouput = document.getElementById('output');
var outHtml = "";

if (rank > 0 && rank < 2) {
  outHtml =  "<p>Cool! You've earned the bronze crown</p>";
} else if ( rank > 2 && rank <= 4) {
 outHtml = "<p>Awesome! You've earned the silver crown</p>";
} else if ( rank === 5) {
  outHtml = "<p> Phenominal! You've earned the gold crown<p/>";
}

var message = '<p>You answered ' + score + ' out of 5 questions correctly! </p>';
output.innerHTML = outHtml + message;
Emanuel Rouse
Emanuel Rouse
1,985 Points

Haha awesome we do share the same name! I see what you're saying and will definitely implement into my code. Thanks!