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

Chris Harkin
Chris Harkin
1,104 Points

Improvments

Hi I completed the conditional challenge and it works correctly. but wanted to improve on my solution by giving the user some feed back if they answers where correct or wrong.

// - keep track of the number of questions the user answered correctly.
var score = 0; 


//1.    It has to ask at least five questions.
var quetsion1 = prompt("Who was the first Finnish driver to win the world championship?");
var quetsion2 = prompt ("In 2005, the rules stated that teams were not allowed to change _____ during a grand prix? ");
var quetsion3 = prompt ("Who was the first driver to win a Grand Prix using a semi-automatic gearbox?");
var quetsion4 = prompt ("He was known as the voice of Formula 1 in the UK. Can you name him?");
var quetsion5 = prompt ("What nationality is Eddie Jordan? ");

// store the answers
var answer1 = "KEKE ROSBERG";
var answer2 = "TYERS";
var answer3 = "NIGEL MANSELL";
var answer4 = "MURRAY WALKER";
var answer5 = "IRISH";

// check the user input against answer
if (quetsion1.toUpperCase() === answer1){
  score += 1;
}

if (quetsion2.toUpperCase() === answer2){
  score += 1;
}

if (quetsion3.toUpperCase() === answer3){
  score += 1; 
}

if (quetsion4.toUpperCase() === answer4){
  score += 1; 
}

if (quetsion5.toUpperCase() === answer5){
  score += 1; 
}


/*
- provide a final message after the quiz, letting the user know the number of questions he or she got right.
- Rank the player.
*/
if(score === 5){
  alert ("congratulations you got " + score + " correct  amswers you won the gold crown");
}else if (score > 2 && score < 5){
  alert ("Thats greate you got " + score + " correct  amswers you won the silver crown");
} else if  (score > 0 && score < 3){
  alert ("you got " + score + " correct  amswers You won the bronze crown"); 
}else{
  alert ("Sorry you got " + score + " correct  amswers  no crown for you");
}  

But if I want to improve on this by giving the user feedback on my if statements where I check if the user entered the correct answer and update the score. I have an Idea on how to do it but the feed back part wont work. This is the part that I tried but cant get to work.

if (quetsion1.toUpperCase() === answer1){
  alert ("Well done thats Correct");
  score += 1;
} else {
  alert ("Sorry the correct answer is" + answer1 );
}

1 Answer

Chris Harkin
Chris Harkin
1,104 Points

sorry that was a typo but didn't effect the code. Thanks for letting me now low I didn't see it as I was more concerned as to why the fed back wouldn't work

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

if (quetsion1.toUpperCase() === answer1){

You're testing to see if the question = answer. Shouldn't you be testing to see if their answer = the real answer?

Chris Harkin
Chris Harkin
1,104 Points

Brendan question1 is the variable that holds the user's answer to question 1. answer1 is the variable that holds the correct answer to question 1

so if user Input (question1) is not the same as answer (answer1) then the it will be wrong. Well at least thats how I designed it and having tested it is working.

apart from the feed back