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 trialedward wong
3,349 PointsImprovements on my code for conditional challenge
My code works but wanted to get thoughts/advice on any improvements I can implement into my code.
var usersAnswer = false;
var correctAnswer = 0;
var firstQ = prompt('What is 12 divided by 4?');
if (parseInt(firstQ) === 3) {
usersAnswer = true;
correctAnswer += 1;
}
var secQ = prompt('What is 48 divided by 8?');
if (parseInt(secQ) === 6) {
usersAnswer = true;
correctAnswer += 1;
}
var thirdQ = prompt('What is 40 divided by 5?');
if (parseInt(thirdQ) === 8) {
usersAnswer = true;
correctAnswer +=1;
}
var fourthQ = prompt('What is 8 didvided by 8?');
if (parseInt(fourthQ) === 1) {
usersAnswer = true;
correctAnswer +=1;
}
var fifthQ = prompt('What is 24 divided by 6?');
if (parseInt(fourthQ) === 1) {
usersAnswer = true;
correctAnswer +=1;
}
if(correctAnswer === 5) {
var rank = 'GOLD';
} else if(correctAnswer === 3 || correctAnswer === 4) {
rank = 'SILVER';
} else if(correctAnswer === 1 || correctAnswer === 2) {
rank = 'BRONZE';
} else {
rank = 'CROWNLESS';
}
alert('You scored ' + correctAnswer + ' out of 5 and are ' + rank + '!');
2 Answers
KRIS NIKOLAISEN
54,972 PointsWhat is usersAnswer for? That can be eliminated and the code behaves the same.
pkay Acharya
Courses Plus Student 5,627 Pointsstart using function. that will elimenate writing long lines of codes.