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 trialElena Torrey
9,156 PointsMy solution to the quiz challenge (also my very first js code). Any feedback would be appreciated!
// Asking five questions
var correctGuess = 0
var answer1 = prompt("What is the longest river in Asia?"); // Yangtze
if (answer1.toUpperCase() === "YANGTZE"){
alert("Correct! Yangtze is the longest river in Asia - 6,300 km - 3,915 miles (6,301 km)");
correctGuess += 1 // keeping track of the number of questions the user answered correctly
} else {
alert("Wrong! Yangtze is the longest river in Asia - 6,300 km - 3,915 miles (6,301 km)");
}
var answer2 = prompt("What is the Highest mountain in Africa?"); // Kilimanjaro
if (answer2.toUpperCase() === "KILIMANJARO") {
alert("Correct! Kilimanjaro the highest mountain in Africa, and rises approximately 4,877 metres (16,001 ft) from its base to 5,895 metres (19,341 ft) above sea level.");
correctGuess += 1
} else {
alert("Wrong! Kilimanjaro the highest mountain in Africa, and rises approximately 4,877 metres (16,001 ft) from its base to 5,895 metres (19,341 ft) above sea level.");
}
var answer3 = prompt("What is the world's largest landlocked country (has no borders on an ocean?");
// Kazakhstan
if (answer3.toUpperCase() === "KAZAKHSTAN") {
alert("Correct! Kazakhstan is the world's ninth largest country but is the world's largest landlocked country.");
correctGuess += 1
} else {
alert("Wrong! Kazakhstan is the world's ninth largest country but is the world's largest landlocked country.");
}
var answer4 = prompt("Which country is the fifth largest in the world by surface area?"); // Brazil
if (answer4.toUpperCase() === "BRAZIL") {
alert("Correct! Brazil is the fifth largest country in the world by surface area after Russia, Canada, China, the United States.");
correctGuess += 1
} else {
alert("Wrong! Brazil is the fifth largest country in the world by surface area after Russia, Canada, China, the United States.");
}
var answer5 = prompt("Which country has the largest population in Africa?"); // Nigeria
if (answer5.toUpperCase() === "NIGERIA") {
alert("Correct! The country with the largest population in Africa is Nigeria, with 154,729,000 inhabitants.");
correctGuess += 1
} else {
alert("Wrong! The country with the largest population in Africa is Nigeria, with 154,729,000 inhabitants.");
}
// letting the player know how many correct answers they got
document.write("<h1>You got " + correctGuess + " questions right!</h1>");
// ranking the player
if (correctGuess === 5) {
document.write("<h2>You earned a GOLD crown</h2>");
} else if (correctGuess >= 3) {
document.write("<h2>You earned a SILVER crown</h2>");
} else if (correctGuess >= 1) {
document.write("<h2>You earned a BRONZE crown</h2>");
} else {
document.write("<h2>Sorry, today is not your day!</h2>");
}
2 Answers
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsHi Elena Torrey .
Just to give you a heads up you are missing a } at the end of your code :)
But the feedback from me ain't going to be that bad :)
My suggestion would only be that you change your "alert"s to "document.write"s . As you code looks now it's a bit distracting when a window pops up and either warns or gives you praise.
That info should be subtly printed to the page, so that the game isn't interrupted every time you give an answer. It's like playing Call of Duty and when shooting (clicking left mouse button) the gun shoots but the game is then paused.
Another way would be to ask the questions and give the info about user answers at the end of the game. That would be even better because the user concentrates on the question to come and not get self-esteem drop when the game prints that the user answered incorrectly. As you know all of us want to do best in a quiz. It's our nature to be constantly thriving to be better.
You'll become better as you go that's for sure.
But none the less the game was interesting to play. Nice questions!
Happy coding!!!
Elena Torrey
9,156 PointsThank you Nejc for taking time to look at my code. I agree and my initial plan was to print the answer right or wrong to the page but when the browser prints the answer the next alert box pops up rightaway and you can't even see the answer to the previous question. I couldn't figure out how to fix it, so I printed the answers in the alert boxes. I think I will go ahead and use your second suggestion: to give the answers at the very end.
Thank you for your heads up!!!
Elena Torrey
9,156 PointsThank you, Nejc!
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsNejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsEdited your post for syntax formatting.