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

Can anyone help me with my challenge attempt

heres the code that i wrote, im having trouble with the score part, how to add 1 score to the "score" variable if i quessed right, and remove 1 if wrong,

i put the else "score = 0" if question is not answered correct, but does'nt that set the whole score back to 0?

var questions = firstQuestion + secondQuestion + thirdQuestion + fourthQuestion + fifthQuestion;

var firstQuestion = prompt("Whats the capital city of sweden");

var secondQuestion = prompt("Whats the capital city of Norway");

var thirdQuestion = prompt("Whats the capital city of Denmark");

var fourthQuestion = prompt("Whats the capital city of Finland");

var fifthQuestion = prompt("Whats the capital city of England");

var score = 0

if (firstQuestion === "stockholm") {
   score += 1
} else {
  score -= 0
}

if (secondQuestion === "Oslo") {
   score += 1
} else {
  score -= 1
}

if (thirdQuestion === "Kopenhagen") {
   score += 1
} else {
  score -= -1 
}

if (fourthQuestion === "Helsinki") {
   score += 1
} else {
  score -= -1 
}

if (fifthQuestion === "London") {
   score += 1
} else {
  score -= -1
}


if (score === 5) {
  alert(" Congratulaions you earned a golden crown!");
} else if (score === 4 || score === 3) {
  alert("Good job your earned a Silver crown!");
} else if (score === 2 || score === 1) {
  alert("Good, you earned a bronze crown!");  
} else {
  document.write("You did not answer any questions right, you did not earn a crown!"); 
}

5 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Erdrag.

I think the problem here is a pure maths problem.

When you write score -= -1 you are in fact adding a point even if the answer is wrong. It is the same as score += 1, so it turn out that if you go wrong on every single question you actually score 2 in total.

Let me ask you a question:

when user's answer is wrong, do you want to add 0 to the points total or you want to subtract 1? I ask this because in the Stockholm question you go with zero, while it seems that in the other questions you want to subtract 1. Please note that in only the else code for Oslo actually subtracts.

Let me know if it makes sense.

Vittorio

Hey thanks for replying so Quick, yea i want to subtract 1 if a questions is wrong, i put the 0 in the Stockholm questions because i did not know if it was good to subtract 1 if you havent got any points, wouldnt that make the score -1?

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Erdrag.

Yes that would make score = -1, but this is covered by the final else statement when you provide the result. So 0 and -1 would both write "You did not answer any questions right, you did not earn a crown!" on the document.

Vittorio

Hey again, lets say i answer the first questions wrong so it subtract 1 from the score, so now its -1 then i answer the second question right so i get +1 in score so now i have 0 in score, when i should have 1

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

So this means that you don't want to subtract 1 point when they go wrong with a question right? That's why I asked.

If you want to subtract 1 when they go wrong and add 1 when the answer correctly, in the case you just wrote the result has to be 0 and not 1. (-1 from first question and +1 from second question equals 0).

Otherwise, if you want to have score = 1 with a wrong answer + a right answer, you don't have to penalize them subtracting 1 when they go wrong; in this case you can just get rid of the al the else statement that subtract 1 if a answer is wrong (those are the one with score -= 1 inside).

Which one is the option you want?

Hey , i want to add 1 score if a question is answered correctly, and not give any point if a question is answered wrong,

thats why i did "if the question is answered correctly" score += 1 to add 1 score, (dont know if its the correct way to do it)

but i had problem to figure out how to do if you answered wrong, so i tried subtracting 1, but i did not work,

i tried to remove the else statements, so when a question is answered wrong it does not change the score, but when i had all questions correct, it said i got a silver crown in the end, so im not sure what i am doing wrong here.

// erdrag

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Please paste the whole code you have at the point.

Hello, i removed the score -1 at the else statements, and now it works, thanks for explaining!:) heres the code, tell me what you think about it:P

var score = 0;

var firstQuestion = prompt("Whats the capital city of sweden?");

if (firstQuestion.toLowerCase() === "stockholm") {
   score += 1;
   alert("Correct, you have earned 1 point");
} else {
  alert("Wrong! the correct answer is stockholm");
}

var secondQuestion = prompt("Whats the capital city of Norway?");

if (secondQuestion.toLowerCase() === "oslo") {
   score += 1;
  alert("Correct, you have earned 1 point");
} else {
  alert("Wrong! the correct answer is oslo");
}

var thirdQuestion = prompt("Whats the capital city of Denmark?");

if (thirdQuestion.toLowerCase() === "kopenhagen") {
   score += 1;
  alert("Correct, you have earned 1 point");
} else {
  alert("Wrong! the correct answer is kopenhagen");
}

var fourthQuestion = prompt("Whats the capital city of Finland?");

if (fourthQuestion.toLowerCase() === "helsinki") {
   score += 1;
  alert("Correct, you have earned 1 point");
} else {
  alert("Wrong! the correct answer is helsinki");
}

var fifthQuestion = prompt("Whats the capital city of England?");

if (fifthQuestion.toLowerCase() === "london") {
   score += 1;
  alert("Correct, you have earned 1 point");
} else {
  alert("Wrong! the correct answer is london");
}


if (score === 5) {
  alert(" Congratulaions you earned a golden crown!");
} else if (score === 4 || score === 3) {
  alert("Good job your earned a Silver crown!");
} else if (score === 2 || score === 1) {
  alert("Good, you earned a bronze crown!");  
} else {
  document.write("You did not answer any questions right, you did not earn a crown!"); 
}


if(alert) {
  alert("Refresh the page to try again")
  document.write('<A HREF="javascript:location.reload(true)">Reload the page!</A>') 
}
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hey!

I see a few typos here and there, and some spacing which is not constant throughout the code, but in general the code does its job.

;)

Vittorio