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 = 0??? why

i try to test my first approuch but each result is 0... why?

var pregunta1 = prompt ('¿Mencione como se llama su planeta?'); var pregunta2 = prompt ('¿Mencione cual es su continente?'); var score = 0; if (pregunta1 === 'tierra'){ score + 20 } else { score - 20} if (pregunta2 === 'america'){ score + 20 } else { score - 20} document.write (score);

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! It's because you've never really changed the original value of score. Let's look at an example from your code:

if (pregunta1 === 'tierra') {
 score + 20 
}

The problem here is that it does that calculation. But the result of the calculation is never stored anywhere. What you're looking for, I believe is to have the original score incremented by 20 and then reassigned into score. There are a couple of ways you can do this.

You can do it like this:

score += 20;

Or:

score = score + 20;

I prefer the first way as it is shorter and more concise, but they do the same thing. They take score add 20 to it and then store it back into score. The same thing will need to be done for your else statements where someone loses points as well. Those can be done in the same fashion.

Either:

score -= 20;

Or:

score = score - 20;

Hope this helps! :sparkles:

Alejandro Rodriguez
Alejandro Rodriguez
3,020 Points

Thanks you so much! your are right the only issue if not work is the -= 20; for some reason add -20 + -20 = -40 so i just use in the else condition just -20 instead of -=20... do you know why?

let me show you:

var score = 0; if (pregunta1.toUpperCase() === 'TIERRA'){ score += 20 } else { score -= 20} if (pregunta2.toUpperCase() === 'AMERICA'){ score += 20 } else { score -= 20}

If i answer one correct and one wrong i get -40 ??? but if i use } else { score - 20} sustract in teh correct way.

thanks for your time and help

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'm not sure what to tell you. It works for me. When I get one wrong and one correct, I get a score of 0. When I get two correct, I get 40. When I get 2 incorrect, I get negative 40. I'll show you the code I have. Keep in mind, though that I added some semicolons and some nice formatting :smiley:

var pregunta1 = prompt ('¿Mencione como se llama su planeta?');
var pregunta2 = prompt ('¿Mencione cual es su continente?'); 

var score = 0; 
if (pregunta1.toUpperCase() === 'TIERRA'){
     score += 20;
} else { 
    score -= 20;
} 
if (pregunta2.toUpperCase() === 'AMERICA'){
     score += 20;
} else {
    score -= 20;
}

document.write (score);

See if this works for you. If it doesn't, let me know your results and what web browser you're using. :sparkles: