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 JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge Solution

Variable named correct

Hi I'm confused on how the correct variable starts off at 0 and then for each question += 1.

2 Answers

Steven Parker
Steven Parker
231,248 Points

On line 5, "let correct = 0;" creates the variable and sets it to 0 to begin with.

Then, on lines 20, 24, 28 ...etc. the "correct += 1" raises the value by one each time, but the "if" statement only lets this happen if the answer was right.

Does that clear it up?

Mateusz Dobrzynski
Mateusz Dobrzynski
1,631 Points

I had similar issue with line twenty as undefined. problem solved when I assigned value to 1 after first answer :

let score = 0;

// 2. Store the rank of a player let rank = '';

// 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 answer1 = prompt("Name a city we first met"); if ( answer1.toUpperCase() === 'LONDON' ) { correct = 1 ; } const answer2 = prompt('Name a district we first met'); if ( answer2.toUpperCase() === 'SHOREDITCH' ) { correct +=1 ; } const answer3 = prompt('What month did we meet'); if ( answer3.toUpperCase() === 'MAY' ) { correct +=1 ; } const answer4 = prompt('What month did we get married'); if ( answer4.toUpperCase() === 'AUGUST' ) { correct +=1 ; } const answer5 = prompt('Where did we spend our honeymoon'); if ( answer5.toUpperCase() === 'COLOMBIA'|| answer5.toUpperCase() === 'PANAMA' ) { correct +=1 ; }
Steven Parker
Steven Parker
231,248 Points

The problem here is the variable was created with the name "score", but in the questions it is referred to as "correct".

The name "correct" was never propertly declared or initialized.

Mateusz Dobrzynski
Mateusz Dobrzynski
1,631 Points

Let correct = 0, got it . Thanks for a quick reply !