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 trialGreg Taylor
798 PointsCan I do it this way? Video Conditional Challenge Solution
/*
1. Store correct answers
- When quiz begins, no answers are correct
*/
let correctAnswer = "";
// 2. Store the rank of a player
let rank = "";
// 3. Select the <main> HTML element
let rightAnswers = "";
/*
4. Ask at least 5 questions
- Store each answer in a variable
- Keep track of the number of correct answers
*/
let question1 = prompt("Type python");
if (question1 === "python"){
correctAnswer +=1
}
let question2 = prompt("Type css");
if (question2 === "css"){
correctAnswer +=1
}
let question3 = prompt("Type html");
if (question3 === "html"){
correctAnswer +=1
}
let question4 = prompt("Type javascript");
if (question4 === "javascript"){
correctAnswer +=1
}
let question5 = prompt("Type ruby");
if (question5 === "ruby"){
correctAnswer +=1
}
/*
5. Rank player based on number of correct answers
- 5 correct = Gold
- 3-4 correct = Silver
- 1-2 correct = Bronze
- 0 correct = No crown
*/
rank
if( +correctAnswer.length === 5){
rank += "Gold"
}else if( +correctAnswer.length === 4 || +correctAnswer === 3){
rank += "Silver"
}else if ( +correctAnswer.length === 1 || +correctAnswer === 2 ){
rank += "Bronze"
} else {
}
let msg = `<h1><strong>You got ${correctAnswer.length} out of 5 questions correct</strong></h1>
Crown earned: <strong>${rank}</strong>`
// 6. Output results to the <main> element
document.querySelector("main").innerHTML = msg;
1 Answer
Steven Parker
231,271 PointsAt first glance I notice that the answer count is created as a string (let correctAnswer = ""
) and then later is concatenated with a digit (correctAnswer +=1
) potentially several times. This is confusing as it appears as it is being incremented as a number.
It would be better to create it as a number to begin with (let correctAnswer = 0
). Then later instead of checking the string length (if (+correctAnswer.length === 5)
), the value could be checked directly (if (correctAnswer === 5)
).
And for future questions, a better way to show your code is by making a snapshot of your workspace and posting just the link to it here.