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

Sharing my quiz

This challenge was really fun. Sharing the quiz I made below, I'd appreciate if you leave feedback.

I'm asking questions to the user about capitals of five countries. The program is keeping score for the correct answers and wrong answers and finally awards the player a medal. Also counts down remaining questions..

alert ("I will now ask you 5 questions");

var userScore = 0
var questions = 5
var questionsLeft = "[" + questions + "] questions left."

var question1 = prompt("What is the capital of England? " + questionsLeft );
if (question1.toUpperCase() === "LONDON") { userScore = userScore + 1
}


questions -= 1
questionsLeft = "[" + questions + "] questions left."


var question2 = prompt("What is the capital of Russia? " + questionsLeft );
if (question2.toUpperCase() === "MOSCOW") {  userScore = userScore + 1
}

questions -= 1
questionsLeft = "[" + questions + "] questions left."


var question3 = prompt("What is the capital of Greece? " + questionsLeft );
if (question3.toUpperCase() === "ATHENS") {  userScore = userScore + 1
}

questions -= 1
questionsLeft = "[" + questions + "] questions left."


var question4 = prompt("What is the capital of Germany? " + questionsLeft );
if (question4.toUpperCase() === "BERLIN") {  userScore = userScore + 1
}

questions -= 1
questionsLeft = "[" + questions + "] questions left."


var question5 = prompt("What is the capital of France? " + questionsLeft );
if (question5.toUpperCase() === "PARIS") {  userScore = userScore + 1
}

var wrongAnswers = 5 - userScore
alert ("You gave " + userScore + " correct answers and "  + wrongAnswers + " wrong answers.");


var userMedal = ""

if (userScore < 1) { userMedal = "Participation Medal"}
if (userScore > 0 && userScore < 3) { userMedal = "Bronze Medal"}
if (userScore > 2 && userScore < 5) { userMedal = "Silver Medal"}
if (userScore > 4) { userMedal = "Gold Medal"}


alert ("You got the " + userMedal + "! Congratulations!")

1 Answer

Hi,

Your code works great! The medal system is a nice touch.

Some suggestions:

You should end your statements with a semi-colon. If you copy and paste your code into JS Hint it will show you which lines have missing colons. Its an awesome tool for finding problems and writing cleaner code.

You are repeating yourself a lot, which is normally a good time to use a function. You should try to make your code as DRY as possible. Functions are covered in the Javascript basics course

Add some comments to explain what your code is doing. It helps other people, and also helps you when you come back to it later and realise you've forgotten how it works.

Hope this helps :)

Thanks for the suggestions James! I didn't know about JS Hint, sounds like a great tool indeed.

Got a quick question for you since you're a Moderator :)

I am unable to post this under the relevant video's discussion, it always ends up being posted under the general JavaScript forum without the proper tags to the relevant video. Any ideas how I can add tags to the relevant topic? So far I've tried posting right under the video with the Get Help button and I also tried navigating to the relevant discussion page and asking there, both times my post appeared without any tags.

Thanks!

Hmm thats weird. I wasn't able to reproduce your problem, I went to this page and used the 'Get Help' link under the video. It added the tags ok for me.

Unfortunately, I don't think there's a way for you to add the tags after you've already posted. Can't do it as a mod either. Only thing I would suggest is to edit your post and add a link to the video manually.

See Guide to markdown for adding links and other formatting.

Hope this helps :)