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 trialMitchell Smit
9,219 PointsJust want to know if i did a good job or maybe need to be better
Hey everyone,
I would like someone to check if my could be better then this or if this is good enough
//collecte coins
var collect = 0;
//count
var countAnswers = 5;
var countWrongAnswers = 0;
//questions
var answer1 = prompt("What is my favorite color?").toUpperCase();
if (answer1 === "BLACK") {
countAnswers -= 1;
alert("You guessed the right answer!" + " [ " + countAnswers + " Questions Left ]");
collect += 1;
} else {
countAnswers -= 1;
countWrongAnswers += 1;
alert("This was the wrong answer!" + " [ " + countAnswers + " Questions Left ]");
}
var answer2 = prompt("What is the Capital City of Holland?").toUpperCase();
if (answer2 === "AMSTERDAM") {
countAnswers -= 1;
alert("You guessed the right answer!" + " [ " + countAnswers + " Questions Left ]");
collect += 1;
} else {
countAnswers -= 1;
countWrongAnswers += 1;
alert("This was the wrong answer!" + " [ " + countAnswers + " Questions Left ]");
}
var answer3 = prompt("Who is the singer of the song:" + " Beat it!").toUpperCase();
if (answer3 === "MICHAEL JACKSON") {
countAnswers -= 1;
alert("You guessed the right answer!" + " [ " + countAnswers + " Questions Left ]");
collect += 1;
} else {
countAnswers -= 1;
countWrongAnswers += 1;
alert("This was the wrong answer!" + " [ " + countAnswers + " Questions Left ]");
}
var answer4 = prompt("What programming language am i using for this quiz?").toUpperCase();
if (answer4 === "JAVASCRIPT") {
countAnswers -= 1;
alert("You guessed the right answer!" + " [ " + countAnswers + " Questions Left ]");
collect += 1;
} else {
countAnswers -= 1;
countWrongAnswers += 1;
alert("This was the wrong answer!" + " [ " + countAnswers + " Questions Left ]");
}
var answer5 = prompt("What is the name of this learning platform?").toUpperCase();
if (answer5 === "TREEHOUSE" || answer5 === "TEAM TREEHOUSE" || answer5 === "TEAMTREEHOUSE") {
countAnswers -= 1;
alert("You guessed the right answer!" + " [ " + countAnswers + " Questions Left ]");
collect += 1;
} else {
countAnswers -= 1;
countWrongAnswers += 1;
alert("This was the wrong answer!" + " [ " + countAnswers + " Questions Left ]");
}
// After game results
if ( collect === 5) {
document.write("Very Good you won the game!" + " [ " + collect + " GOOD QUESTIONS ] " + " [ " + countWrongAnswers + " WRONG QUESTIONS ] ");
} else if ( collect === 4 || collect === 3) {
document.write("Good you won the game!" + " [ " + collect + " GOOD QUESTIONS ] " + " [ " + countWrongAnswers + " WRONG QUESTIONS ] " + " Next time Better!");
} else if ( collect === 2 || collect === 1) {
document.write("Mmmm you losed the game!" + " [ " + collect + " GOOD QUESTIONS ] " + " [ " + countWrongAnswers + " WRONG QUESTIONS ] " + " Next time Better!");
} else {
document.write("I'm Sorry but you've lost!" + " [ " + collect + " GOOD QUESTIONS ] " + " [ " + countWrongAnswers + " WRONG QUESTIONS ] " + " Next time Better!");
}
2 Answers
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsA couple suggestions:
1)
You could make use of the includes()
method that exists on both arrays and strings. So this...
if (answer5 === "TREEHOUSE" || answer5 === "TEAM TREEHOUSE" || answer5 === "TEAMTREEHOUSE") {}
...could be refactored like this:
if (['TREEHOUSE', 'TEAM TREEHOUSE', 'TEAMTREEHOUSE'].includes(answers5) {}
...or even like this, since the answer string should always include 'TREEHOUSE'
if (answer5.includes('TREEHOUSE') {}
2)
You can also make use of template strings. This:
alert("You guessed the right answer!" + " [ " + countAnswers + " Questions Left ]");
...is the same as this:
alert(`You guessed the right answer![ ${countAnswers} Questions Left ]`)
Mitchell Smit
9,219 PointsThanks I will try this!
Steven Parker
231,110 PointsIf you've done thorough functional testing, and the program works as required, that in itself is the definition of "good enough" in most cases. I didn't confirm it, but at first glace it seems you've already done this.
But there's also nearly always ways to make things better. If the program already works right, you might still be able to make it more compact and/or easier to maintain. For example, one thing you could do with this code is to create functions for the recurring tasks to get the same job done with less code. This is a technique that you will find covered in some of the other courses.
Mitchell Smit
9,219 PointsThanks💪🏼
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsWould you mind editing your question so that it's easier to read your code? Here are some tips.