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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2

Danilo Rodriguez
Danilo Rodriguez
1,663 Points

'Unexpected end of script'

I asked a question but I quickly found an answer. Here's the code anyway:

//Prints in specific location
function print(message){
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}
//Builds a list from an array
function buildList(results){
  var listHTML = '<ol>';
   for (var i=0; i<results.length; i+=1){
     listHTML += '<li>' + results[i] + '</li>';}
  listHTML += '</ol>';
  return listHTML;
}

//Variables for quiz here
var questions = [
  ["What's my first name?","Danilo"],
  ["What's my middle name?","Armand"],
  ["What's my last name?", "Rodriguez"]
]
 var questionsGood = [];
 var questionsBad = [];
 var correctAnswers = 0;
 var wrongAnswers = 0;
 var asked;
 var answered;
 var goodAnswer;

//Asks array questions and keeps track of how many and which questions are right and wrong
for (var i = 0; i < questions.length; i +=1){
  var asked = questions[i][0];
  var goodAnswer = questions[i][1];
  var answered = prompt(asked);
    if (answered === goodAnswer){
    correctAnswers += 1;
      questionsGood.push(asked);
    } else {wrongAnswers +=1; questionsBad.push(asked)}
}
//If some questions are correct and some wrong this message displays
if (correctAnswers >= 1 && wrongAnswers >= 1){
var closeMessage = 'You got ' + '<strong>'+ correctAnswers +'</strong>'+ ' right and ' +'<strong>'+ wrongAnswers +'</strong>'+ ' wrong.'
closeMessage += '<h2> The questions you answered ' + '<strong>' + 'correctly' + '</strong>' + ' are: </h2>';
closeMessage += buildList(questionsGood);
closeMessage += '<h2> The questions you answered ' + '<strong>' + 'wrong' + '</strong>' + ' are: </h2>';
closeMessage += buildList(questionsBad);
print(closeMessage);}
//If all questions are correct this message displays
else if (wrongAnswers === 0){
  var allRight = '<strong>Congratulations! You answered all questions correctly!!!</strong>';
  print(allRight);}
//If all questions are wrong this message displays
else {
  var allWrong = 'bruh. ' + 'You got ' + '<strong>ALL</strong>' + ' the questions wrong.'
  print(allWrong);}
Danilo Rodriguez
Danilo Rodriguez
1,663 Points

It was because I tried to test my code after adding an 'If' and 'else if' without a finishing 'else'

1 Answer

Steven Parker
Steven Parker
229,744 Points

Actually, a final "else" following an "if/else...if" chain is optional.

I did not see the original code, but I might guess that the issue may have been mismatched braces.