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

My solution - good one? Feedback please!

What do you think about my solution for the quiz.js challenge? Some things to do better?

var question1 = prompt('Does ruby use gems? (y/n)');
var question2 = prompt('Is an integer a number? (y/n)');
var question3 = prompt('Is an float a number without decimals? (y/n)');
var question4 = prompt('Opens document.write a window? (y/n)');
var question5 = prompt('Will i get a really good JS-developer? (y/n)');

var correct_answers = 0;

if (question1.toLowerCase() === "y") {
  correct_answers += 1;
}
if (question2.toLowerCase() === "y") {
  correct_answers += 1;
}
if (question3.toLowerCase() === "n") {
  correct_answers += 1;
}
if (question4.toLowerCase() === "n") {
  correct_answers += 1;
}
if (question5.toLowerCase() === "y") {
  correct_answers += 1;
}

if (correct_answers === 5) {
  document.write('You got ' + correct_answers + ' correct answers and won the gold crown! WOW!');
} else if (correct_answers === 4 || correct_answers === 3) {
  document.write('You got ' + correct_answers + ' correct answers and won the silver crown! Good!');
} else if (correct_answers === 2 || correct_answers === 1) {
  document.write('You got ' + correct_answers + ' correct answers and won the bronce crown! That's okay.');
} else {
  document.write('You got ' + correct_answers + ' correct answers. I think you need to learn more.');
}

2 Answers

I noticed your last else if block in the last if statement has a syntax error on the string.

The apostrophe on this's is closing your string before the string actually ends. You can fix this by changing the single quotes on that string to double quotes or using an escape character (backward slash) in front of the apostrophe (this\'s).

double quotes:

document.write('You got ' + correct_answers + " correct answers and won the bronce crown! That's okay.");

escape character:

document.write('You got ' + correct_answers + ' correct answers and won the bronce crown! That\'s okay.');

Aaaah, thank you Robby Faller!