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
Mario Garcia de Leon Recio
3,288 PointsCan anyone give me some tips to fix my code? :-)
// 5 questions var question1 = prompt('How many fingers do you have in one hand?'); var question2 = prompt('How many fingers do you have in two hands?'); var question3 = prompt('How many eyes do you have?'); var question4 = prompt('how many days have a week?'); var question5 = prompt('What is the color of the sky?');
// Answers and Scores var answer1 = 5; var answer2 = 10; var answer3 = 2; var answer4 = 7; var answer5 = 'blue';
if (parseInt.question1 === answer1) { var score1 = 1; } else { score1 = 0; } if (parseInt.question2 === answer2) { var score2 = 1; } else { score2 = 0; } if (parseInt.question3 === answer3) { var score3 = 1; } else { score3 = 0; } if (parseInt.question4 === answer4) { var score4 = 1; } else { score4 = 0; } if (question5 === answer5) { var score5 = 1; } else { score5 = 0; }
/* We'll let the player know how many questions did he/she got right. And we'll show that using document.write() */ var totalScore = score1 + score2 + score3 + score4 + score5; document.write("You've got " + totalScore + " questions right");
//rank the players and show the rewards var goldCrown = 5; var silverCrown = 3; var silverCrownToo = 4; var bronzeCrown = 1; var bronzeCrownToo = 2;
if (totalScore === goldCrown) { alert('You have earned the Golden Crown!'); } else if (totalScore === silverCrown || totalScore === silverCrownToo) { alert('You have earned the Silver Crown!'); } else if (totalScore === bronzeCrown || totalScore === bronzeCrownToo) { alert('You have earned the Bronze Crown!'); } else { alert('You should try it again!'); }
5 Answers
tobiaskrause
9,160 PointsSoooo i formated your code...I haven't the time yet to completely check your code BUT with the first look i already noticed, that your "ifs" wont work.
Every else uses a score...but what is a score? It isn't declared :) Dont declare your variables in these if statements. Do it before the if like you did it with the answer variables
Mario Garcia de Leon Recio
3,288 PointsHi Tobias,
Thank you very much! I appreciate your quick answer! I understand now better some of the mistakes + how to post code into the community chat...
I've been working on a new code: https://w.trhou.se/w3t5zqi969
That works for me now ;-)
tobiaskrause
9,160 PointsYour new code is much better (unless the user lost a hand/eye). Easier to read + not so much dead code
Mario Garcia de Leon Recio
3,288 Pointsindeed ;-)
tobiaskrause
9,160 PointsBut I would recommend you to only use one question variable...you dont need question 1 - 5 you only need one since the value of that varaible always changes. Always look at efficiency :)
Mario Garcia de Leon Recio
3,288 PointsLet's see...
// Variable "answer"
var answer = 0;
// 5 questions
var question = prompt('How many fingers do you have in one hand?');
if (question === '5') {
answer += 1;
}
question = prompt('How many fingers do you have in two hands?');
if (question === '10') {
answer += 1;
}
question = prompt('How many eyes do you have?');
if (question === '2') {
answer += 1;
}
question = prompt('how many days have a week?');
if (question === '7') {
answer += 1;
}
question = prompt('What is the name of our teacher?');
if (question.toUpperCase() === 'ALEX') {
answer += 1;
}
// Final score
document.write('<p>You have ' + answer + ' questions right!</p>');
// Ranking
if (answer === 5) {
alert('You earned the Golden Crown');
} else if (answer >= 3) {
alert('You earned the Silver Crown');
} else if (answer >= 1) {
alert('You earned the Bronze Crown');
} else {
alert('You should try it again');
}
tobiaskrause
9,160 PointsYep that will work since the varaible question gets a new value with every new prompt. And this new value can be checked in the next if statement ;)
Mario Garcia de Leon Recio
3,288 PointsThanks Tobias for your quick support! Much appreciate it!
tobiaskrause
9,160 Pointstobiaskrause
9,160 PointsPost your code like this...see more in the Markdown Cheatsheet Its just pain (for the eyes) to read your code since you didn't formated it :D sry