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

Bohdan Afanasyev
Bohdan Afanasyev
2,104 Points

Unexpected token {

Greetings, I've decided to spend sometime experimenting with code and cant figure out why its not working on line 51, can you please have a look and explain what has been done wrong.

var correctAnswers = 0;

var questionOne = prompt('How many seconds in a minute?'); if (questionOne.toLowerCase() === 'sixty' || questionOne === '60') { correctAnswers += 1;
alert('You\'ve answered right ' + correctAnswers + ' of 5 questions.'); } else { alert('You guessed wrong. And have answered right ' + correctAnswers + ' of 5 questions.');
}

var questionTwo = prompt('How many minute in a hour?'); if (questionTwo.toLowerCase() === 'sixty' || questionTwo === '60') { correctAnswers += 1;
alert('You\'ve answered right ' + correctAnswers + ' of 5 questions.'); } else { alert('You guessed wrong. And have answered right ' + correctAnswers + ' of 5 questions.');
}

var questionThree = prompt('How many hours in a day?'); if (questionThree.toLowerCase() === 'twenty four' || questionThree === '24') { correctAnswers += 1;
alert('You\'ve answered right ' + correctAnswers + ' of 5 questions.'); } else { alert('You guessed wrong. And have answered right ' + correctAnswers + ' of 5 questions.');
}

var questionFour = prompt('How many days in a week?'); if (questionFour.toLowerCase() === 'seven' || questionFour === '7') { correctAnswers += 1;
alert('You\'ve answered right ' + correctAnswers + ' of 5 questions.'); } else { alert('You guessed wrong. And have answered right ' + correctAnswers + ' of 5 questions.');
}

var questionFive = prompt('How many weeks in a month?'); if (questionFive.toLowerCase() === 'four' || questionFive === '4') { correctAnswers += 1;
alert('You\'ve answered right ' + correctAnswers + ' of 5 questions.'); } else { alert('You guessed wrong. And have answered right ' + correctAnswers + ' of 5 questions.');
}

if (correctAnswers === 0) {
alert('Its such a pitty that you haven\'t answered any of our questions, you should learn more and come back again!'); }

if (correctAnswers === 1 || correctAnswers === 2) {
  reward = 'Bronze Medal'; }
  var prepareReward = prompt('Well done, you have completed the test, are you ready to get your reward?');
  if (prepareReward.toLowerCase() === 'y' || prepareReward.toLowerCase() === 'yes') {
    document.write('<p>Our congratulations you have gained ' + reward + '!</p>'); }
  else (prepareReward.toLowerCase() === 'n' || prepareReward.toLowerCase() === 'no') {
   document.write('<p>Our team would like to thank you for participating in this test, have a nice day!</p>'); }

if (correctAnswers === 3 || correctAnswers === 4) {
  reward = 'Silver Medal';
  var prepareReward = prompt('Well done, you have completed the test, are you ready to get your reward?');}
  if (prepareReward.toLowerCase() === 'y' || prepareReward.toLowerCase() === 'yes') {
    document.write('<p>Our congratulations you have gained ' + reward + '!</p>');}
  else (prepareReward.toLowerCase() === 'n' || prepareReward.toLowerCase() === 'no'){ 
    document.write('<p>Our team would like to thank you for participating in this test, have a nice day!</p>');}

if (correctAnswers === 5) {
  reward = 'Gold Medal';
  var prepareReward = prompt('Well done, you have completed the test, are you ready to get your reward?');}
  if (prepareReward.toLowerCase() === 'y' || prepareReward.toLowerCase() === 'yes') {
    document.write('<p>Our congratulations you have gained ' + reward + '!</p>');}
  else (prepareReward.toLowerCase() === 'n' || prepareReward.toLowerCase() === 'no'){ 
    document.write('<p>Our team would like to thank you for participating in this test, have a nice day!</p>');}

3 Answers

Jitendra Bansal
Jitendra Bansal
9,805 Points

If you want to write condition after else you need to use this format

if (condition) { } else if (condition) { }

You have done it written it like this (which is not correct.

else (condition) { }

Erwin Meesters
Erwin Meesters
15,088 Points

Instead of else try else if.

if (condition) {
    block of code to be executed if the condition is true
} else { 
    block of code to be executed if the condition is false
}

You wrote a condition after your else statement. That's not ok. So or remove the condition or write else if instead of else

Sean Henderson
Sean Henderson
15,413 Points

The actual problem is on line 53, I think the interpreter stops at the last line it can comprehend.

// ...
} else(prepareReward.toLowerCase() === 'n' || prepareReward.toLowerCase() === 'no') {
// ...

In Javascript, else does not accept any conditions (in parenthesis). I think you meant to use else if (...), which would look like this:

// ...
} else if (prepareReward.toLowerCase() === 'n' || prepareReward.toLowerCase() === 'no') {
// ...

For example, here's a basic function to check an answer:

if (input === answer) {
  console.log('correct!')
} else {
  console.log('the input is not exactly equal to the answer, and that\'s all I know.')
};

Above, else is used as a catch-all for any instance where the answer is not exactly true. We could add and else if block to handle more scenarios:

if (input === answer) {
  console.log('correct!');
} else if (input === answer -1 || input === answer + 1) {
  console.log('so close! try again.');
} else {
  console.log('the input is not exactly equal to the answer, and that\'s all I know.');
};

Here we added a message that tells the user they were very close, and not to inspires them to forge ahead with zeal and confidence. Because we kept the else block in tact, there will never be a situation where this program doesn't know what to do.