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 Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Alexandru Vlasnita
Alexandru Vlasnita
5,542 Points

My code

I've done some overkill around the crowns part, but will it affect the performance in situations like this? P.S: I know it looks messy, i'm using VSC and it all looks clean over there but when i post it here the lines just hug eachother for some reason.

let correctAnswers = 0;

let rank = "No Crown :(";

let question1 = prompt(`What's 5 * 5?`); 
let question2 = prompt(`Is JavaScript an OOP language?`);
let question3 = prompt(`What's the capital of England?`);
let question4 = prompt(`In which city is the Eiffel Tower located?`);
let question5 = prompt(`Which car manufacturer makes the X5 model?`);

if ( question1 == 25 ) {
    correctAnswers += 1;
}

if (question2.toLowerCase() === 'yes' || question2.toLowerCase() == 'y') {
    correctAnswers += 1;
}

if ( question3.toLowerCase() === 'london') {
    correctAnswers += 1;
}

if ( question4.toLowerCase() === 'paris' ) {
    correctAnswers += 1;
}

if ( question5.toLowerCase() === 'bmw' ) {
    correctAnswers += 1;
}

if ( correctAnswers == 1 || correctAnswers == 2 ) {
    rank = 'Bronze';
} else if (correctAnswers == 3 || correctAnswers == 4 ) {
    rank = 'Silver';
} else if (correctAnswers == 5 ) {
    rank = 'Gold';
}

let result = `<h2>You've got ${correctAnswers} out of 5 questions correct.</h2>
              <p>Crown earned: <strong>${rank}</strong></p> `;

document.querySelector("main").innerHTML = result;

What if someone answered the first question with "twenty-five"? Would they still get that question correct and if so, is it because you did not make the value of question1 strictly equal to 25?

Or would they get it wrong because you did not give it an OR operator?

twenty-five would not be counted as a correct answer in this case

1 Answer

surround your code in triple backticks, then it will look better.

let correctAnswers = 0;

let rank = "No Crown :(";

let question1 = prompt(What's 5 * 5?); let question2 = prompt(Is JavaScript an OOP language?); let question3 = prompt(What's the capital of England?); let question4 = prompt(In which city is the Eiffel Tower located?); let question5 = prompt(Which car manufacturer makes the X5 model?);

if ( question1 == 25 ) { correctAnswers += 1; }

if (question2.toLowerCase() === 'yes' || question2.toLowerCase() == 'y') { correctAnswers += 1; }

if ( question3.toLowerCase() === 'london') { correctAnswers += 1; }

if ( question4.toLowerCase() === 'paris' ) { correctAnswers += 1; }

if ( question5.toLowerCase() === 'bmw' ) { correctAnswers += 1; }

if ( correctAnswers == 1 || correctAnswers == 2 ) { rank = 'Bronze'; } else if (correctAnswers == 3 || correctAnswers == 4 ) { rank = 'Silver'; } else if (correctAnswers == 5 ) { rank = 'Gold'; }

let result = <h2>You've got ${correctAnswers} out of 5 questions correct.</h2> <p>Crown earned: <strong>${rank}</strong></p>;

document.querySelector("main").innerHTML = result;

Also, I think it's fine performance wise, although it seems you forgot some quotation marks.

Alexandru Vlasnita
Alexandru Vlasnita
5,542 Points

Thanks for the answer, the missing quotes were probably lost while i was trying to make it look better on here, back in VSC everthing is ok