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

Rebecca Goodman
Rebecca Goodman
2,902 Points

Feedback on code.

I've worked with compiled languages a lot. It's hard for me to wrap my head around not needing to declare my variables up front. Code works but, is this format frowned upon? Like most, I'd probably do this with an array and a loop in any other circumstance, but I'm trying to follow along with the course. Tried to markup code with fences, let's see if it works.

// Prime variables and prompt for input
var question1 = prompt("What symbol is the assingment operator in JavaScript?");
var userGuess1 = question1;
var answer1 = "=";
var question2 = prompt("Yes or No: Java is the same as JavaScript?");
var userGuess2 = question2.toUpperCase();
var answer2 = "NO";
var question3 = prompt("This data type returns only true or false values, what is it?");
var userGuess3 = question3.toUpperCase();
var answer3 = "BOOLEAN";
var question4 = prompt("This data type is normally surrounded by quote marks, what is it?");
var userGuess4 = question4.toUpperCase();
var answer4 = "STRING";
var question5 = prompt("This data type is a whole number either positive, negative or 0, but NOT a fraction, what is it?");
var userGuess5 = question5.toUpperCase();
var answer5 = "INTEGER";
var correctQuestions = 0;

// Calculate and print score
if (userGuess1 === answer1) {
  correctQuestions += 1;
} else {
   correctQuestions += 0;
}

if (userGuess2 === answer2) {
  correctQuestions += 1;
}else {
   correctQuestions += 0;
}

if (userGuess3 === answer3) {
  correctQuestions += 1;
}else {
   correctQuestions += 0;
}

if (userGuess4 === answer4) {
  correctQuestions += 1;
}else {
   correctQuestions += 0;
}

if (userGuess5 === answer5) {
  correctQuestions += 1;
}else {
   correctQuestions += 0;
}
document.write("You scored " + correctQuestions + " points! ");

// Determine and print score and ranking
if (correctQuestions > 4) {
  document.write("Congratulations! You won the gold crown!");
}else if (correctQuestions > 2 && correctQuestions < 5) {
  document.write("Congratulations! You won the silver crown!");
} else if (correctQuestions > 0 && correctQuestions < 3) {
  document.write("Congratulations! You won the bronze crown!");
} else {
   document.write("Sorry! Try again to win a crown.");
}

1 Answer

Overall looks good to me, especially for completing the JavaScript Basics course.

If you are on the JavaScript track you will definitely be able to revisit this one in the future and recognise things that can be improved upon or optimised.

Only change I would make at this stage is to remove the else statement that adds 0 as it is not needed and just adds extra lines.

Nice work, stick with the track, you will love it :)

Rebecca Goodman
Rebecca Goodman
2,902 Points

Thanks man, I appreciate the input!