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!
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
olu adesina
23,007 Pointschanging boolean values in two dimensional arrays ?
hi guys
using a for loop i'm attempting to change 1 of 3 values in an array from false to true is this possible
/var questionsAnswers contains three values. A question and answer and false value which will change when the correct answer is submited///
var questionsAnswers =[ ['What is the capital of England? ','LONDON',false],
['what is the capital of France?','PARIS',false],
['What organ pumps blood around the body?','HEART',false]
];
var ask; var rightAnswers = 0
function print(message) { document.write(message);
}
for(x=0;x<questionsAnswers.length;x+=1) { ask=prompt(questionsAnswers[x][0]);
if(ask.toUpperCase()==questionsAnswers[x][1]) { questionsAnswers[x][2]=true // this is where the false statment is meant to change to true
rightAnswers+=1;
}
}
print('you got '+rightAnswers+' questions right!<BR><BR>')
print('YOU GOT THESE QUESTIONS CORRECT <br><br>')
for(y=0;y<questionsAnswers.length;y+=1) {
/*****************************************
the if statement below is meant to test
if the 3rd value of each array is true
and print the 1st value if it is but
it is printing all values even if wrong
************************************/
if(questionsAnswers[y][2]==true);
{print(questionsAnswers[y][2]+'<br><br>')}
}
1 Answer

andren
28,550 PointsThis if
statement runs regardless of the conditions due to the semicolon you have placed in its declaration. Once JavaScript encounters the semicolon it considers the if
statement to be finished. And will therefore treat the rest of the code as being completely independent of the if
statement.
If you remove it like this
if(questionsAnswers[y][2]) { // removed ; that came after parenthesis
print(questionsAnswers[y][2]+'<br><br>')
}
Then your if
statement should work the way you want it to.
I also removed the == true
part of the condition since it is unnecessary. questionsAnswers[y][2]
will either be true
or false
on its own, so you don't need to compare it with anything.
Steven Parker
224,848 PointsSteven Parker
224,848 PointsWhen posting code, please use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area.