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 trialSimon Safferson
4,150 PointsReview code for improvements
Hi there!
Not sure if code reviews are allowed but I'm a beginner with Javascript and would love some feedback on the code for this project I created. Here is the code:
This is 'The Conditional Challenge' part of the Javascript basics course.
let correctAnswers = 0;
const MaxScore = 5;
const answer1 = 20;
const answer2 = 10;
const answer3 = 50;
const answer4 = 60;
const answer5 = 100;
let rank = 'Rank: ';
const main = document.querySelector('main');
const firstQuestion = prompt("What's 10+10?");
if (+firstQuestion === answer1) {
correctAnswers +=1 ; }
const secondQuestion = prompt("What is 20 divided by 2?");
if (+secondQuestion === answer2) {
correctAnswers +=1 ; }
const thirdQuestion = prompt("What's 25 * 2?");
if (+thirdQuestion === answer3) {
correctAnswers +=1; }
const fourthQuestion = prompt("What's 30 * 2?");
if (+fourthQuestion === answer4) {
correctAnswers +=1; }
const fifthQuestion = prompt("What's 50 * 2?");
if (+fifthQuestion === answer5) {
correctAnswers +=1; }
if (correctAnswers === 5) {
rank +='Gold'; }
if (correctAnswers === 4 || correctAnswers === 3) {
rank +='Silver'; }
if (correctAnswers === 2 || correctAnswers === 1) {
rank +='Bronze'; }
if (correctAnswers === 0) {
rank +='No crown'; }
const Score = `<p><strong> You got ${correctAnswers} out of ${MaxScore}!</strong> </p>`
const finalRank = `<p> Your final ${rank} </p>`
main.innerHTML = Score + finalRank ;
1 Answer
Joseph Yhu
PHP Development Techdegree Graduate 48,637 Points// These could be reduced to just 1 if block if you are familiar with arrays and for loops.
const firstQuestion = prompt("What's 10+10?");
if (+firstQuestion === answer1) {
correctAnswers +=1 ; }
const secondQuestion = prompt("What is 20 divided by 2?");
if (+secondQuestion === answer2) {
correctAnswers +=1 ; }
const thirdQuestion = prompt("What's 25 * 2?");
if (+thirdQuestion === answer3) {
correctAnswers +=1; }
const fourthQuestion = prompt("What's 30 * 2?");
if (+fourthQuestion === answer4) {
correctAnswers +=1; }
const fifthQuestion = prompt("What's 50 * 2?");
if (+fifthQuestion === answer5) {
correctAnswers +=1; }
if (correctAnswers === 5) {
rank +='Gold'; }
/* You could use else if, and the condition could just be correctAnswers >= 3.
If the user got all the questions right, only the first if block would run, so you don't have to worry about them getting both the rank of gold and silver.
*/
if (correctAnswers === 4 || correctAnswers === 3) {
rank +='Silver'; }
// Essentially the same as above except the condition would be correctAnswers >= 1.
if (correctAnswers === 2 || correctAnswers === 1) {
rank +='Bronze'; }
// You don't need any conditions; you could just use else.
if (correctAnswers === 0) {
rank +='No crown'; }
// You forgot to put semicolons for these two lines.
const Score = `<p><strong> You got ${correctAnswers} out of ${MaxScore}!</strong> </p>`
const finalRank = `<p> Your final ${rank} </p>`
Simon Safferson
4,150 PointsSimon Safferson
4,150 PointsSome really valueble insights :) Thanks Joseph, reading up on these now.