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

My code is slightly different to what is shown, are there any disadvantages to the way I have solved it?

Hi -

I've completed the following code for this solution. It's slightly different, like I set my first variable as 0 instead of empty. Are there any distinct disadvantages to doing it like I have?

*/ let correctAnswers = false; console.log(correctAnswers);

let playerRank = 0; console.log(playerRank);

const main = document.querySelector('main');

let question1 = prompt('What is the capital of France?'); if (question1.toUpperCase() === 'PARIS') { correctAnswers = true; console.log(correctAnswers = 'You scored!'); playerRank = playerRank + 1; console.log(playerRank); } let question2 = prompt('What is the capital of the Spain?'); if (question2.toUpperCase() === 'MADRID') { correctAnswers = true; console.log(correctAnswers = 'You scored!'); playerRank = playerRank + 1; console.log(playerRank); } let question3 = prompt('What is the capital of Italy?'); if (question3.toUpperCase() === 'ROME') { correctAnswers = true; console.log(correctAnswers = 'You scored!'); playerRank = playerRank + 1; console.log(playerRank); } let question4 = prompt('What is the capital of Germany?'); if (question4.toUpperCase() === 'BERLIN') { correctAnswers = true; console.log(correctAnswers = 'You scored!'); playerRank = playerRank + 1; console.log(playerRank); } let question5 = prompt('What is the capital of the United States?'); if (question5.toUpperCase() === 'WASHINGTON') { correctAnswer = true; console.log(correctAnswers = 'You scored!'); playerRank = playerRank + 1; console.log(playerRank); }

let finalRank = '';

if (playerRank === 0) { finalRank = 'No crown'; console.log(finalRank); } else if (playerRank > 0 && playerRank <= 2) { finalRank = 'Bronze'; console.log(finalRank); } else if (playerRank >= 3 && playerRank <= 4) { finalRank = 'Silver'; console.log(finalRank); } else if (playerRank === 5) { finalRank = 'Gold'; console.log(finalRank); }

main.innerHTML = <h2>You got ${playerRank} out of 5!</h2> <p>Crown earned: ${finalRank}</p> ;

1 Answer

Steven Parker
Steven Parker
231,008 Points

You forgot to provide a reference to what you are comparing this to, but in your case "playerRank" is a number so initializing it with 0 is correct. It wouldn't make sense to initialize it as an empty string (or a space), and doing that would cause an incorrect result.

On the other hand "final‍Rank" is a string. so its initialization is also correct.