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

What is wrong with this JS code

Uncaught SyntaxError: Unexpected string

var score = 0;

var answer1 = prompt("what is the capital city of Croatia");
if(answer1.toUpperCase() === "ZAGREB") {
score += 1;
}

var answer2 =  prompt("what is the capital city of Italy");
if(answer2.toUpperCase() === "MILANO") {
score += 1;
}

var answer3 =  prompt("what is the capital city of Turkey");
if (answer3.toUpperCase() === "INSTAMBUL") {
score += 1;
}

// this line --- document.write("great you got" + score " correct answers out of 3");

if(score === 3) {
    document.write("great, you have won gold medal");
} else if (score === >=1) {
    document.write("Nice, you have won silver medal");
} else {
    document.write("Hm... you should study geography more");
}

6 Answers

Hey Dominik,

I see your problem. You are missing a + operator in between the variable score and " correct answers out of 3." So, it should be:

document.write("great you got " + score + " correct answers out of 3");

Also, as Julian mentioned, be sure to remove the === from the else if statement.

That's where one of the problems was lol

Here is the entirety of your fixed code:

var score = 0;

var answer1 = prompt("what is the capital city of Croatia");
if(answer1.toUpperCase() === "ZAGREB") {
score += 1;
}

var answer2 =  prompt("what is the capital city of Italy");
if(answer2.toUpperCase() === "MILANO") {
score += 1;
}
//Changed INSTAMBUL TO ISTANBUL, correct spelling
var answer3 =  prompt("what is the capital city of Turkey");
if (answer3.toUpperCase() === "ISTANBUL") {
score += 1;
}
//Added + between score and beginning " for concatenation
document.write("Great! You got " + score + " correct answers out of 3.<br>");

if (score === 3) {
    document.write("Great, you have won gold medal");
//Removed === that causes error in below else if statement
} else if (score >=1) {
    document.write("Nice, you have won silver medal");
} else {
    document.write("Hm... you should study geography more");
}

I tried in Sublime text then I realized maybe I shuld try in workspace and still nothing. Code is ALMOST exactly the same like code challenge solution and still nothing,,, drives me crazy... Does code work for you?

Julian Gutierrez
Julian Gutierrez
19,325 Points

Just to make sure; you have an html file that the script is writing your solution to, correct?

hm... In sublime text I have index.html, and it should work in workspace because I just deleted previous code from code challenge and paste this one... Not sure, weird. I will try it once again tommorow. it is not something important to me, I just tried to replicate code challenge on my own.... Thank you very much for your patience :)

Julian Gutierrez
Julian Gutierrez
19,325 Points

I don't think you need the triple equals sign in your else if statement.

else if (score >=1) {
    document.write("Nice, you have won silver medal");
}

I don't, but still, I have same problem, according to debugger problem is in document.write("great you got" + score " correct answers out of 3"); line

Julian Gutierrez
Julian Gutierrez
19,325 Points

Can I ask where you are coding your solution? Workspaces?

Julian Gutierrez
Julian Gutierrez
19,325 Points

The only other issue I can think of, other than the two previous edits mentioned, is that if your script doesn't have an html file to "document.write" to it will spit out an error. Ran your code here with edits and seems to working correctly. http://codepen.io/anon/pen/pvQxxv