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 Solution

Juan Álvarez
Juan Álvarez
10,859 Points

Output Rank

if ( correctAnswers === 0 ) { document.write("<p>Very bad. You answered " + correctAnswers + " questions correct.</p>"); } else if (correctAnswers === 1 || correctAnswers === 2 ) { document.write("<p>BRONCE CROWN. You answered " + correctAnswers + " questions correct.</p>"); } else if (correctAnswers === 3 || correctAnswers === 4 ) { document.write("<p>SILVER CROWN. You answered " + correctAnswers + " questions correct.</p>"); } else { document.write("<p>GOLD CROWN. You answered " + correctAnswers + " questions correct.</p>"); }

// I would like to know if this way is also correct, I mean sufficiently optimized. Thank you!

Adam Beer
Adam Beer
11,314 Points

Please show your whole code.

Code

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

      ```html
      <p>This is code!</p>
      ```
Juan Álvarez
Juan Álvarez
10,859 Points
var correctAnswers = 0;
var question1 = prompt("What is the capital of Spain");
var question2 = prompt("What is the capital of France");
var question3 = prompt("What is the capital of Portugal");
var question4 = prompt("5 + 4 = ?");
var question5 = prompt("5 * 4 = ?");

if ( question1.toUpperCase() === "MADRID" ) {
   correctAnswers += 1;
}
if ( question2.toUpperCase() === "PARIS" ) {
  correctAnswers += 1;
}
if ( question3.toUpperCase() === "LISBON" ) {
  correctAnswers += 1;
}
if ( parseInt(question4) === 9 ) {
  correctAnswers += 1;
}
if ( parseInt(question5) === 20 ) {
  correctAnswers += 1;
}

if ( correctAnswers === 0 ) {
  document.write("<p>Very bad. You answered " + correctAnswers + " questions correct.</p>");
} else if (correctAnswers === 1 || correctAnswers === 2 ) {
  document.write("<p>BRONCE CROWN. You answered " + correctAnswers + " questions correct.</p>");
} else if (correctAnswers === 3 || correctAnswers === 4 ) {
  document.write("<p>SILVER CROWN. You answered " + correctAnswers + " questions correct.</p>");
} else {
  document.write("<p>GOLD CROWN. You are the master. You answered " + correctAnswers + " questions correct.</p>");
}

Thank you!

Adam Beer
Adam Beer
11,314 Points

I think this is a correct code, nice job! I don't think there's a bug in it. Happy coding!

1 Answer

Steven Parker
Steven Parker
231,275 Points

For "Basics" course code, this looks pretty good to me. :+1:

For now, the focus is on general language syntax, vocabulary, and common control structures. Optimization is a bit more advanced and later courses will cover techniques that will be useful for that.

Juan Álvarez
Juan Álvarez
10,859 Points

Thank you both for your comments.