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 Solution

Heriberto Nieves
Heriberto Nieves
2,466 Points

javascript quiz questions

the instructions were to make a quiz with 5 questions and keep track of the ones that user get right and at the end tell the user how many they got right and give them some sort of price so i did this: for the tracking i did a score variable were each question right awards 20 points to the score variable for a total of 100 points for perfect score 1 to 2 right an alert showing the points and a text the same for 3 to 4 right, i saw the way the teacher did it and he did it with 2 steps less than me per questions, its common to miss a few things like this ones ? i feel i should have known a little better... here is my code:

var question1;
var question2;
var question3;
var question4;
var question5;
var score = 0;

question1 = prompt('Is the sea blue?');
question1 = question1.toUpperCase();

if (question1 === 'NO') {
    alert('CORRECT!');
    score += 20;
} else {
  alert('sorry! the correct answer is no.'); 
}

question2 = prompt('Can a car only use gasoline?');
question2 = question2.toUpperCase();

if (question2 === 'NO') {
    alert('CORRECT!');
    score += 20;
} else {
  alert('sorry! the correct answer is no.'); 
}

question3 = prompt('Does it rain diamonds in jupiter?');
question3 = question3.toUpperCase();

if (question3 === 'YES') {
    alert('CORRECT!');
    score += 20;
} else {
  alert('sorry! the correct answer is yes.');
}

question4 = prompt('Are dolphin mamals?');
question4 = question4.toUpperCase();

if (question4 === 'YES') {
    alert('CORRECT!');
    score += 20;
} else {
  alert('sorry! the correct answer is yes.'); 
}

question5 = prompt('Is Javascript a programming language?');
question5 = question5.toUpperCase();

if (question5 === 'YES') {
    alert('CORRECT!');
    score += 20;
} else {
  alert('sorry! the correct answer is yes.'); 
}

if (score === 100){
  alert('Your Score is ' + score);
  alert('YOU ARE AWESOME, YOU DID PERFECT');
} else if (score <= 80 && score > 60) {
  alert('Your Score is ' + score);
  alert('Not too shabby!');
} else if (score <= 60 && score > 40) {
  alert('Your Score is ' + score);
  alert('Getting there keep it up!');
} else if (score <= 40 && score > 0) {
  alert('Your Score is ' + score);
  alert('Come on, i know you can do a lot better, try again!');
} else {
  alert('Your Score is ' + score);
  alert('You must be sleeping aren\'t you?');
}
Heriberto Nieves
Heriberto Nieves
2,466 Points

oops, how do i post the code nicely?

//Fixed Code Presentation

3 Answers

Shawn Denham
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree
Shawn Denham
Python Development Techdegree Student 17,802 Points

Yes that is totally normal. The point right now is that you can follow and write the logic need to complete the task. As you progress through the JavaScript courses you will learn way to write more compact and cleaner code. But like I said, right now I would just focus on fully understanding concepts and logic being presented!

Hope this helps! -Shawn

Heriberto Nieves
Heriberto Nieves
2,466 Points

thanks shawn and chyno thanks for the post fix appreciate it

Sarah Jee Watson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Sarah Jee Watson
UX Design Techdegree Graduate 28,197 Points

Can anyone show how to do just that? Make this code DRY? I have a multiple choice quiz and want to tell the user if they got the answer right or wrong every time they answer. So they click one of the choices then 'Check Answer' and get to know right away. It also should keep track of the number they got right. I have a CodePen that shows what I have so far: http://codepen.io/SarahJee/pen/jqpxzG Any help much appreciated!