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
francisanthonyl
2,535 PointsHi there I'm stuck on the Conditional Challenge on the JavaScript Track: Here is my code and what am I doing wrong? :
//Ask at least five questions var question1 var question2 var question2 var question4 var question5
//Keep track of the number of questions the user answered correctly var currentscore = 0
//Provide a final message after the quiz letting the user know the number of questions he or she got right.
//Rank the player. If the player answered all five correctly, give that player the gold crown: 3-4 is a silver crown; 1-2 correct answers is a bronze crown and 0 correct is no crown at all.
//QUESTIONNUMBER 1 alert('Answer these questions!') var question1 = prompt('Give me one Koda color?'); if (question1.toUpperCase() === 'BLACK' || question1.toUpperCase() === 'WHITE'){ alert('Correct!') currentscore += 1 } else { document.write("<p>Wrong</p>"); }
//QUESTIONNUMBER 2 alert('Second question! ' + "Current Score:" + currentscore ) var question2 = prompt('How old is Anton Lopez?'); if (question2.toUpperCase() === '28'){ alert('Correct!') currentscore += 1 } else { alert("<p>Wrong</p>"); }
//QUESTIONNUMBER 3 alert('Third question! ' + "Current Score:" + currentscore ) var question3 = prompt('Is he Asian?'); if (question3.toUpperCase() === 'YES'){ alert('Correct!') currentscore += 1 } else { alert("Wrong! No points for you!"); }
//QUESTIONNUMBER 4 alert('Fourth question! ' + "Current Score:" + currentscore ) var question4 = prompt('Is he cool?'); if (question4.toUpperCase() === 'YES'){ alert('Correct!') currentscore += 1 } else { alert("Wrong! No points for you!"); }
//QUESTIONNUMBER 5 alert('Fifth question! ' + "Current Score:" + currentscore ) var question5 = prompt('Where does he live?'); if (question5.toUpperCase() === 'BACOLOD' || question5.toUpperCase() === 'BACOLOD CITY'){ alert('Correct!') currentscore += 1 } else { alert("Wrong! No points for you!"); } var crowncolor
if (currentscore=5) { crowncolor='Golden Crown'; } else if ( parseInt(currentscore) === 3 || parseInt(currentscore) === 4); { crowncolor='Silver Crown'; } else { crowncolor='No Crown'; } } var finalscore ="<h2> CONRATULATIONS! YOU GET THE " + crowncolor + '.</h2>'; document.write(finalscore)
2 Answers
Logan Schmitt
1,090 PointsI just finished this project a few days ago. I notice you added a lot of extra code, which isn't wrong, I like the way you set up your program to work, but it does look like it made more room for bugs. I'll try to keep it all as unchanged as possible while I note them.
First thing to add is to declar your currentScore variable. It can't add 1 to the variable if it hasn't been declared at the very beginning of the program. Don't forget to set it to = 0
QuestionNumber1: I didn't add an alert before my questions, I simply started asking them and I also didn't add a correct/wrong prompt after each, but I like it. I notice that there's no semi colon after the first alert, after the "correct!" alert, or the current score alert. Also, the "wrong!" alert probably doesn't need a <p> tag and should probably be an alert() instead of a document.write since all the rest are alerts. I didn't even use an if/else at the end, all I did was have the program keep track of the score and ask the questions back to back. Maybe something to consider to tidy up the code if you want.
QuestionNumber2: No semi colon after the first alert, the "correct" alert, or the currentScore +=1 update. Same as Question1 for the "wrong" alert (no <p> tag).
QuestionNumber3: Same as Question 2 (semi colons missing same places).
QuestionNumber4: Same as Question 2 (semi colons missing same places).
QuestionNumber5: Same as Question 2 (semi colons missing same places).
Crowncolor: On the first if statement the currentScore should be set to === 5. There's only one = sign. I don't think you need to parseInt the scores. They should already be saved as a number within the variable, they aren't stings so this seems redundant. I also used comparison operators to determine crown color instead of using an || or statement (example: if(currentScore === 5){document.write("You got a gold");} else if(currentScore >= 3){document.write(You got a silver);} else if....... etc.
Here is my code if you would like to see how I did it. Like I said, it's a lot simpler but seems to work ok.
// Asked 5 questions
var correct = 0;
var ans1 = prompt("What color do you get when you mix red and blue?");
if (ans1.toUpperCase() === 'PURPLE') {
correct += 1;
}
var ans2 = prompt("What color do you get when you mix yellow and blue?");
if (ans2.toUpperCase() === 'GREEN') {
correct += 1;
}
var ans3 = prompt("What day comes after Monday?");
if (ans3.toUpperCase() === 'TUESDAY') {
correct += 1;
}
var ans4 = prompt("What month comes after January?");
if (ans4.toUpperCase() === 'FEBRUARY') {
correct += 1;
}
var ans5 = prompt("What interval is between the notes C3 and C4?");
if (ans5.toUpperCase() === 'OCTAVE') {
correct += 1;
}
//Provide message letting know how many answers right
document.write("<p>You got " + correct + " out of 5 questions correct.</p>");
//Rank players by Gold, Silver, Bronze, or no crown
if (correct === 5) {
document.write("<p><strong>You earned a Gold Crown!</strong></p>");
} else if (correct >= 3) {
document.write("<p><strong>You earned a Silver Crown!</strong></p>");
} else if (correct >= 2) {
document.write("<p><strong>You earned a Bronze Crown!</strong></p>");
} else {
document.write("<p><strong>No crown for you. You need to study!</strong></p>");
}
francisanthonyl
2,535 PointsAwesome! Thanks for the fast reply and I have to say you're one of the people that makes the Treehouse community amazing! Thanks for all your help Logan!
Logan Schmitt
1,090 PointsYou're welcome! Glad I could help!