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 (Retired) Making Decisions with Conditional Statements The Conditional Challenge Solution

frits vd Velden
frits vd Velden
1,860 Points

I want to colorise a list with right and wrong answers

I'm trying to color right answers green and wrong answers red. I got the right answers to be green but the red doesn't seem to work. Anybody any ideas? P.s. last block of my code.

Here's my code.

// Aantal antwoorden goed
var answerCorrect = 0;

// De vragen, wanneer de vraag goed is komt er +1 bij de score
var vraagEen = prompt('Hoe heet Frits zijn wederhelft?');
var antwoordEen = vraagEen;
if (vraagEen.toUpperCase() === 'MARIJE' || vraagEen.toLocaleUpperCase() === 'GILLES') {
  answerCorrect += 1;
}

var vraagTwee = prompt('Hoe heet de band van Paul, Rein, Menno en Frits?');
var antwoordTwee = vraagTwee;
if (vraagTwee.toUpperCase() === 'DEEREJOHN' || vraagTwee.toUpperCase() === 'DEERE JOHN') {
  answerCorrect += 1;
}

var vraagDrie = prompt('Zie je Frans, zie je ....');
var antwoordDrie = vraagDrie;
if (vraagDrie.toUpperCase() === 'GERRIE') {
  answerCorrect += 1;
}

var vraagVier = prompt('Als Gilles geen sociaal werker zou zijn dan zou hij wel een ..... zijn');
var antwoordVier = vraagVier;
if (vraagVier.toUpperCase() === 'BEROEPSOPVOEDER') {
  answerCorrect += 1;
}

var vraagVijf = prompt('Hoe heet een erectie in het Duits');
var antwoordVijf = vraagVijf;
if (vraagVijf.toUpperCase() === 'STRAMMER') {
  answerCorrect += 1;
}

// Toon hoeveel antwoorden er goed zijn
document.write('<h1>Je hebt ' + answerCorrect + ' vragen goed. </h2>');

// Bericht bij aantal vragen goed
if (answerCorrect === 5) {
  document.write('<h1>Zo! Jij bent een filantroop! Je weet echt alles van alles!</h1>');
}
else if (answerCorrect >= 3) {
  document.write('<h1>Niet gek gozer. Jij kiest thuis zeker ook de gordijnen?</h1>');
}
else if (answerCorrect >= 1) {
  document.write('<h1>Probeer anders eens een boterham te eten met mes en vork</h1>');
}
else {
  document.write('<h1>Niets goed, geen zorgen</h1>');
}

// Ingevulde antwoorden
document.write('<h1>Jouw antwoorden op een rijtje</h1>');

if (vraagEen) {
  document.write('<li><p>' + antwoordEen.fontcolor('green') + '</p></li>');
}
else {
  document.write('<li><p>' + antwoordEen.fontcolor('red') + '</p></li>');
}
if (vraagTwee) {
  document.write('<li><p>' + antwoordTwee.fontcolor('green') + '</p></li>');
}
else {
    document.write('<li><p>' + antwoordTwee.fontcolor('red') + '</p></li>');
}
if (vraagDrie) {
  document.write('<li><p>' + antwoordDrie.fontcolor('green') + '</p></li>');
}
else {
    document.write('<li><p>' + antwoordDrie.fontcolor('red') + '</p></li>');
} 
if (vraagVier) {
  document.write('<li><p>' + antwoordVier.fontcolor('green') + '</p></li>');
}
else {
    document.write('<li><p>' + antwoordVier.fontcolor('red') + '</p></li>');
}
if (vraagVijf) {
  document.write('<li><p>' + antwoordVijf.fontcolor('green') + '</p></li>');
}
else {
    document.write('<li><p>' + antwoordVijf.fontcolor('red') + '</p></li>');
}

4 Answers

Charles Wanjohi
Charles Wanjohi
9,235 Points

The problem is with your if condition.You need to compare the value of the variable with the correct answer. i.e

if (vraagEen.toUpperCase() === 'MARIJE' || vraagEen.toLocaleUpperCase() === 'GILLES') {
  document.write('<li><p>' + antwoordEen.fontcolor('green') + '</p></li>');
}else{
document.write('<li><p>' + antwoordEen.fontcolor('red') + '</p></li>');
}

The if(vraagEen) would always return true since the variable is not null i.e it has a string value, the reason all your answers are green.Hope this helps you

Adam Rushton
PLUS
Adam Rushton
Courses Plus Student 11,583 Points

Looks like you have the if statements looking at a string. for example, the first if statement is checking if the variable vraagEen is true or false. Only if it is false it will follow the else statement.

by the looks of it the variable vraagEen is actually a string, in which case no matter what you enter, it is actually always true.

You will need to use some test to see if the answer stored in the vraagEen (and all following answer variables) is correct or incorrect.

Hope this helps

frits vd Velden
frits vd Velden
1,860 Points

Right, that makes sense. Thanks! I'll go back to my code and try to figure out how to do this.

frits vd Velden
frits vd Velden
1,860 Points

Just figured out that my own try wasn't flawless, yours is Charles. Thanks!