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

Mosneagu Sebastian
Mosneagu Sebastian
4,518 Points

Why does my code work? :)))

So this is my code and it runs like i expected it to.Except i just realised that the very first line "correctquess = 0" is wrong, wasnt i suposed to write var the first time i create a variable?Why does it work?

correctguess = 0;
var question1 = prompt("Where does the footballer Cristiano ronaldo play?");
question1 = question1.toUpperCase();
if (question1 === "JUVENTUS") {
  correctguess += 1;
}


var question2 = prompt("How old is Jorge Masivdal?");
question2 = question2.toUpperCase();
if (question2 === "34") {
  correctguess += 1;
}

var question3 = prompt("In which country is located the city Venezia?");
question3 = question3.toUpperCase();
if (question3 === "ITALY") {
  correctguess += 1;
}


var question4 = prompt("Who is the main protagonist in the video game The Witcher?");
question4 = question4.toUpperCase();
if (question4 === "GERALT OF RIVIA") {
  correctguess += 1;
}


var question5 = prompt("What does A.I stand for?");
question5 = question5.toUpperCase();
if (question5 === "ARTIFICIAL INTELLIGENCE") {
  correctguess += 1;
}

document.write("You got " + parseInt(correctguess) + " out of 5.  ");

if (parseInt(correctguess) === 5 ) {
  document.write("You won a gooooold medal.")
} else if (parseInt(correctguess) === 4 || parseInt(correctguess) === 3) {
  document.write("You won a silver medal.");
} else if (parseInt(correctguess) === 2 || parseInt(correctguess) === 1) {
  document.write("You won a bronze medal.")
} else {
  document.write("You get no medal.")
}

3 Answers

Jamie Reardon
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jamie Reardon
Treehouse Project Reviewer

JavaScript can be kinda forgiving when declaring variables. So it sort of auto assumes that this line is a variable and declares it for you.

Mosneagu Sebastian
Mosneagu Sebastian
4,518 Points

PS: How can i add my code like everybody else does? Copy-Paste does not look that readable.

when you go to post a comment click the bold text below the input here that says Markdown Cheatsheet. Explains how to post your code.