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

The Conditional Challenge

I believe i accomplished what i was supposed to in the code challenge.

var score = 0;
var questions = 5; 
var questionsLeft = ' [' + questions + ' questions left]';
var question1;
var question2;
var question3;
var question4;
var question5;



question1 = prompt('Is JavaScript A Programming Language?' + questionsLeft );
question1 = question1.toUpperCase();
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';

if (question1 === 'YES') {


    score = score + 20;
}


question2 = prompt('Is the earth round?' + questionsLeft);
question2 = question2.toUpperCase();
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';

if (question2 === 'YES') {


    score = score + 20;

}

question3 = prompt('Is the sky blue?' + questionsLeft);
question3 = question3.toUpperCase();
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';

if (question3 === 'YES') {


    score = score + 20;
}


question4 = prompt('Do you like pizza?' + questionsLeft);
question4 = question4.toUpperCase();
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';

if (question4 === 'YES') {


    score = score + 20;

}

question5 = prompt('Do you like to play this game?' + questionsLeft);
question5 = question5.toUpperCase();

if (question5 === 'YES') {


    score = score + 20;

}




if (score === 100) {

  document.write('You received a perfect score! <br />');
  document.write('You got gold! <br />');
  document.write('You score is: ' + score);

} else if ( score <= 80 && score >= 60 ) {

  document.write('You got silver <br />');
  document.write('You score is: ' + score);

} else if ( score <= 40 && score >= 20 ) {

  document.write('You got bronze! <br />');
  document.write('You score is: ' + score);

} else {

  document.write('You get nothing!');

}
Steven Parker
Steven Parker
231,072 Points

When posting code, be sure to format it using the instructions in the Markdown Cheatsheet pop-up found below the "Add an Answer" area. :arrow_heading_down:

Also, did you have a specific question or issue with this code?

Steven Parker just want to know if i am accomplishing the task, i believe so because i am keeping a track of the questions, prompting the user for the questions, and testing how many he/she got right.

1 Answer

Steven Parker
Steven Parker
231,072 Points

It sounds like you've already determined the program meets the requirements.

But now if you want you could use it as DRY ("Don't Repeat Yourself") coding practice and make it more compact. Notice how some of the code is almost exactly duplicated several times? What if you had the questions and answers stored in array(s) and used a loop to ask and score the questions?

I was trying to do that earlier but i could not get the logic down how i would start to do that. I was thinking about using a for loop to ask the questions but when i did so i looped five times asking the same questions five times.

Steven Parker
Steven Parker
231,072 Points

But what if the questions were in an array, and each time through the loop you used the loop counter as an index into the array?

Steven Parker, thanks! I will have to think a little bit more about this once i get to arrays and loops. Thank you for your idea to help me.