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
Sandy Hadley
4,463 PointsMy code for the JS Conditional Challenge. Bulky, but functional. Any ideas to slim down or otherwise improve it?
HI, everyone! I just completed my code for this challenge, and as a newbie, I've probably done a bunch of stuff that could be done better/DRYer. I'd appreciate anyone who likes taking a gander and any suggestions/criticisms. Here's the code:
/*
Here is a simple 5-question
quiz to help pass the time.
*/
var tally = 0;
var start = prompt("Would you like to play a game?");
if ( start.toUpperCase() === "YES" || start.toUpperCase() === "Y" ) {
alert("Okay, there will be five questions. Let's begin!");
} else {
document.write("Okay - goodbye!");
exit();
}
//Question 1
var answer1 = "1865";
var correctGuess = false;
var question1 = prompt("In which year did former slaves in the United States gain the right to legally marry?");
if ( question1 === answer1 ) {
correctGuess = true;
} else if ( question1 !== answer1 ) {
var guessAgain1 = prompt("Try again. Here's a hint: it was the same year in which the Thirteenth Amendment to the Constitution was passed.");
} if ( guessAgain1 === answer1 ) {
correctGuess = true;
}
if ( correctGuess ) {
document.write("You answered " + answer1 + ". That is correct - excellent!");
tally += 1;
console.log(tally);
} else {
document.write("Sorry. The year was " + answer1 + ".");
}
//Question 2
var answer2 = "2";
var correctGuess = false;
var question2 = prompt("If Peter Piper picked a peck of pickled peppers, how many quarts did Peter Piper pick?");
if ( question2 === answer2 ) {
correctGuess = true;
} else if ( question2 !== answer2 ) {
var guessAgain2 = prompt("Oops, that answer was not correct. Try again!");
} if ( guessAgain2 === answer2 ) {
correctGuess = true;
}
if ( correctGuess ) {
document.write(" Correct, a peck is equal to " + answer2 + " quarts.");
tally += 1;
console.log(tally);
} else {
document.write(" A peck is equal to " + answer2 + " quarts.");
}
//Question 3
var randomNumber = Math.floor(Math.random() * 5) + 1;
var correctGuess = false;
var question3 = prompt("I am juggling between 1 and 5 oranges. Guess how many.");
if ( parseInt(question3, 10) === randomNumber ) {
correctGuess = true;
} else if ( parseInt(question3, 10) < randomNumber) {
var guessAgain3 = prompt("You guessed " + question3 + ". That's a little low. Guess again.");
} if ( parseInt(guessAgain3, 10) === randomNumber) {
correctGuess = true;
} else if ( parseInt(question3, 10) > randomNumber) {
var guessAgain4 = prompt("You guessed " + question3 + ", which is a little high. Guess again.");
} if (parseInt(guessAgain4, 10) === randomNumber) {
correctGuess = true;
}
if (correctGuess) {
document.write(" Yes, I am juggling " + randomNumber + " of 5 oranges - good going!");
tally += 1;
console.log(tally);
} else {
document.write(" Sorry, I am juggling " + randomNumber + " of 5 oranges.");
}
//Question 4
var correctGuess = false;
var question4 = prompt("Here's an easy one. Yes or no: am I wearing pants?");
if ( question4.toUpperCase() === "YES" || question4.toUpperCase() === "Y" ) {
document.write(" Yes, indeed, I am fully pantsed.");
correctGuess = true;
} else {
document.write(" Pants are overrated - you are correct!");
correctGuess = true;
}
if (correctGuess) {
tally += 1;
console.log(tally);
}
//Question 5
var correctGuess = false;
var answer3 = "to thine own self be true";
var question5 = prompt("Final question: according to Polonius in Shakespeare's Hamlet, what, above all things, should you do?");
if ( question5.toLowerCase() === answer3 ) {
correctGuess = true;
} else if ( question5 !== answer3 ) {
var guessAgain5 = prompt("Try again!");
} if ( guessAgain5.toLowerCase() === answer3 ) {
correctGuess = true;
}
if (correctGuess) {
document.write(" You are correct, Polonius said " + answer3 + " . Well done!");
tally += 1;
console.log(tally);
} else {
document.write(" Polonius said " + answer3 + ".");
}
//Tallying the score
document.write(" You correctly answered " + tally + " of 5 questions.");
//Assigning prizes
if ( tally === 5 ) {
document.write(" You earned a gold crown. Congratulations!");
}
else if ( tally >== 3 ) {
document.write(" You earned a silver crown. Good job!");
}
else if ( tally >== 1 ) {
document.write(" You earned a bronze crown. Nice!");
}
else {
document.write("You earned no crown. Thanks for playing!");
}
3 Answers
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsI would have an array of questions and an array of answers. So questions[0] would correspond to answer[0]. Then you should define a function for asking a question which takes a parameter for the number, and than number gets passed in as the index number for your question array and your answer array. You might also need an array for the extra phrases you say when they get the answer right, which are all unique. And the random juggling is a different case. But those are some ideas to work with.
Joshua Seitz
9,942 PointsYou can use a while-loop, but I am sure you will or should learn that next. You can check out example of while-loop at: source: http://www.w3schools.com/js/js_loop_while.asp
Sandy Hadley
4,463 PointsHi Joshua, I'll check that out. As I'm just starting with Javascript, there's a bunch of stuff I haven't been exposed to yet. Thanks for the tip!
Jacob Mishkin
23,118 PointsSandy,
what Brandon said this correct, but if you keep going in the course you will learn how to create 2d arrays. So I would say, keep going and learn about arrays.
Sandy Hadley
4,463 PointsSandy Hadley
4,463 PointsHi Brendan, that is the kind of thing I thought would be possible, but do not yet have the skills to implement. We very briefly touched on arrays at some point in this course, so I'll be reading up on those. Thank you for your ideas!