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 trialkevinkrato
5,452 PointsCan you review my code.
I wrote this code and I tested it out. As far as I am concerned it is perfect. However, a more trained eye could perhaps point out potential mistakes or improvements. If anyone has any suggestions I'd love to see them!
// Questions var q1 = prompt('Which is larger, a mouse or an elephant?'); var q2 = prompt('Which is faster, a cheetah or a turtle?'); var q3 = prompt('Which is taller, a tower or a chair?'); var q4 = prompt('Which is smarter, a human or a brick?'); var q5 = prompt('Which is heavier, a boulder or a feather?');
var correctGuess = parseInt(0); var wrongGuess = parseInt(0);
// Conditions per Question if (q1.toUpperCase() === 'ELEPHANT') { correctGuess += parseInt(1); } else { wrongGuess += parseInt(1); }
if (q2.toUpperCase() === 'CHEETAH') { correctGuess += parseInt(1); } else { wrongGuess = parseInt(1); }
if (q3.toUpperCase() === 'TOWER') { correctGuess += parseInt(1); } else { wrongGuess += parseInt(1); }
if (q4.toUpperCase() === 'HUMAN') { correctGuess += parseInt(1); } else { wrongGuess += parseInt(1); }
if (q5.toUpperCase() === 'BOULDER') {
correctGuess += parseInt(1);
} else {
wrongGuess += parseInt(1);
}
// Results
if (correctGuess === parseInt(3)) { alert('You got 60% correct. Better luck next time!'); } else if (correctGuess === parseInt(4)) { alert('You got 80% correct. Great score, but you can do better!'); } else if (correctGuess === parseInt(5)) { alert('You got 100% of the questions correct! Great job!'); } else { alert('Sorry, you failed the test.'); }
// Awards
if (correctGuess === parseInt(5)) { alert('You have won the Gold Crown!'); } else if (correctGuess === parseInt(4) || correctGuess === parseInt(3)) { alert('You have won the glorious Silver crown!'); } else { alert('You have won a participation ribbon.'); }
console.log(correctGuess); console.log(wrongGuess);
1 Answer
Charles Wanjohi
9,235 PointsYou dont need to use the parseInt function on numbers.Its used to get a number from a string.It should be just ;
correctGuess += 1;
wrongGuess+=1;
etc etc
kevinkrato
5,452 Pointskevinkrato
5,452 PointsYeah I noticed that as I progressed through the course, good stuff.