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

JavaScript

Adam Saulters
Adam Saulters
7,468 Points

Conditions Challenge

Good morning!

I am having trouble with two parts here. The first part I am having a hard time is having my answers to show up after being counted correctly. My The second part that is giving me problems is when it pulls the rank of correct answers it always shows up silver. It counts the answers correctly, but except for it showing "5 correct answers" it pops up "You have gotten 11111 answers correct" and it always populates silver no matter if I get 5 correct or none correct.

I would appreciate any help that yall are able to give. Thanks in advance!

/*

  1. Store correct answers
    • When quiz begins, no answers are correct */ let answerCorrect = ''; let rank = 'norank'; const answerone = '10'; const answertwo = '6'; const answerthree = '12'; const answerfour = '6'; const answerfive = '2';

// 2. Store the rank of a player const rankone = 'Gold'; const ranktwo = 'Silver'; const rankthree = 'Bronze'; const norank = 'None';

// 3. Select the <main> HTML element const main = document.querySelector('main');

/*

  1. Ask at least 5 questions
    • Store each answer in a variable
    • Keep track of the number of correct answers */

const questionone = prompt ('What is 5+5?'); if ( questionone === answerone ) { answerCorrect += 1; }

const questiontwo = prompt ('What is 3+3?'); if ( questiontwo === answertwo ) { answerCorrect += 1; }

const questionthree = prompt ('What is 6+6?'); if ( questionthree === answerthree ) { answerCorrect += 1; }

const questionfour = prompt ('What is 1+5?'); if ( questionfour === answerfour ) { answerCorrect += 1; }

const questionfive = prompt ('What is 1+1?'); if ( questionfive === answerfive ) { answerCorrect += 1; } /*

  1. Rank player based on number of correct answers
    • 5 correct = Gold
    • 3-4 correct = Silver
    • 1-2 correct = Bronze
    • 0 correct = No crown */ if ( answerCorrect === 5) { rank = rankone } else if ( answerCorrect <5 || answerCorrect >2) { rank = ranktwo } else if ( answerCorrect <3 || answerCorrect >0) { rank = rankthree } else { rank = norank }

// 6. Output results to the <main> element main.innerHTML = <h1>You got ${answerCorrect} answers correct.</h1> <p> You recieved a ${rank}!</p>;

console.log(You got ${answerCorrect} answers correct. You recieved a ${rank}!);

2 Answers

Steven Parker
Steven Parker
231,140 Points

The issue is due to this initialization line:

let answerCorrect = '';

This creates the variable and sets it to be an empty string, so later when the code attempts to increment the value, it instead appends a "1" character to the string. To fix this, initialize the variable as a number instead:

let answerCorrect = 0;

And when posting code to the forum, use Markdown formatting (as I did here) to preserve the appearance.

Adam Saulters
Adam Saulters
7,468 Points

Steven,

Thank you for the help! I will make post in markdown in the future.

Adam

Steven Parker
Steven Parker
231,140 Points

Adam Saulters — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!