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

Alejandro Rodriguez
Alejandro Rodriguez
3,020 Points

Score is ever 5... why???

I write this code and i think is ok but my result is ever - something no matters if a answer correct o incorrect why?

//*** EXAMEN Y EVALUACION ***

// Preguntas
var pregunta1 = prompt ('¿Mencione como se llama su planeta?');
var pregunta2 = prompt ('¿Mencione cual es su continente?');
var pregunta3 = prompt ('¿Mencione cual es su país?');
var pregunta4 = prompt ('¿Mencione cual es su provincia?');
var pregunta5 = prompt ('¿Mencione cual es su cantón?');

// Calificación inicial

//Evaluadores
var score = 0;
if (pregunta1.toUpperCase() === 'TIERRA'){
score += 1;
} else { score -= 1}
if (pregunta2.toUpperCase() === 'AMERICA'){
score += 1;
} else { score -= 1}
if (pregunta2.toUpperCase() === 'COSTA RICA'){
score += 1;
} else { score -= 1}
if (pregunta2.toUpperCase() === 'HEREDIA'){
score += 1;
} else { score -= 1}
if (pregunta2.toUpperCase() === 'FLORES'){
score += 1;
} else { score -= 1}

//Medallas
if (score === 5){
  medalla = 'Oro';
}else if (score === 4) {
  medalla = 'Plata';
}else if (score === 3) {
  medalla = 'Bronce';
}else if (score === 2) {
  medalla = 'Alumino';
}else if (score === 1) {
  medalla = 'Papel';
}else {
  medalla = 'Looseeer!';
}

//Mensaje final
document.write ('<h2>Felicidades usted contestó ' + score + ' correctas. Haz ganado una medalla de ' + medalla + '</h2>');

3 Answers

Matthew Long
Matthew Long
28,407 Points

First thing I notice is you have pregunta2 written 4 times instead of checking the answers for pregunta3, pregunta4, and pregunta5.

Alejandro Rodriguez
Alejandro Rodriguez
3,020 Points

Ok you are right! but now my problem is the counter score:

if i answer 5 correct the score is 5 if i answer 4 correct the score is 3 if i answer 3 correct the score is 1 if i answer 1 (or 4 wrong) correct the score is -3 if i answer 5 wrong the score is -5 and so on... any idea???

//*** EXAMEN Y EVALUACION ***

// Preguntas
var pregunta1 = prompt ('¿Mencione como se llama su planeta?');
var pregunta2 = prompt ('¿Mencione cual es su continente?');
var pregunta3 = prompt ('¿Mencione cual es su país?');
var pregunta4 = prompt ('¿Mencione cual es su provincia?');
var pregunta5 = prompt ('¿Mencione cual es su cantón?');

// Calificación inicial

//Evaluadores
var score = 0;
if (pregunta1.toUpperCase() === 'TIERRA'){
score += 1;
} else { score -= 1}
if (pregunta2.toUpperCase() === 'AMERICA'){
score += 1;
} else { score -= 1}
if (pregunta3.toUpperCase() === 'COSTA RICA'){
score += 1;
} else { score -= 1}
if (pregunta4.toUpperCase() === 'HEREDIA'){
score += 1;
} else { score -= 1}
if (pregunta5.toUpperCase() === 'FLORES'){
score += 1;
} else { score -= 1}

//Medallas
if (score === 5){
  medalla = 'Oro';
}else if (score === 4) {
  medalla = 'Plata';
}else if (score === 3) {
  medalla = 'Bronce';
}else if (score === 2) {
  medalla = 'Alumino';
}else if (score === 1) {
  medalla = 'Papel';
}else {
  medalla = 'Looseeer!';
}

//Mensaje final
document.write ('<h2>Felicidades usted contestó ' + score + ' correctas. Haz ganado una medalla de ' + medalla + '</h2>');
Matthew Long
Matthew Long
28,407 Points

The way you have written your code is if you get a question correct the score goes up by one, and if you get the question wrong the score goes down by one. If this is not desired, and you only want the score to go up for correct answer, and stay the same for an incorrect answer, then remove the else clause containing score -= 1. This leaves you with the following, for example.

if (pregunta1.toUpperCase() === 'TIERRA') {
  score += 1;
}
Alejandro Rodriguez
Alejandro Rodriguez
3,020 Points

But in my case i want score up by one or down by one that is why score -= 1 but is not working right... yiu are right the easy way is just delete the line else but i want add and subtract values. But something is wrong... any idea?

Matthew Long
Matthew Long
28,407 Points

In that case if you answer 4 correct then your score is 3, and if you answer 3 correct then the score is 1, etc. By decreasing the score when getting an answer incorrect this is the result you would expect. Also, this is how the code works now. I would not want to be graded like this, by the way... :stuck_out_tongue_winking_eye:

However, if this is not what you're expecting then I am not following, and I am sorry for my confusion. Could you give me the scores you are expecting?

Alejandro Rodriguez
Alejandro Rodriguez
3,020 Points

Ok first thanks you so much for take time to answer me.

Ok here is the deal

If you answer 5 right get 5 If you answer 4 right and 1 wrong get 4 If you answer 3 right and 2 wrong get 3

All this because I give a award ?for each score 5 gold, 4 silver, 3 bronze, 2 aluminum, 1 paper and 0 looseeer! LOL

but my counter is no working fine.

Matthew Long
Matthew Long
28,407 Points

Then you need to remove the else clause and stop subtracting a point from score for every missed answer.

Basically, what you're wanting is to start at a score of zero and add one to the score for each correct answer. I would suggest you remove the else clause and go watch the solution video. I think his solution, which is basically the same as yours, if you remove the else clause, will explain things better. Good luck!