Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Mitchell Smit
Mitchell Smit
9,219 Points

Just 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,696 Points

A 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
Mitchell Smit
9,219 Points

Thanks I will try this!

Steven Parker
Steven Parker
216,175 Points

If 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. :+1:

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.