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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

i am not sure why it is saying unexpected end of input when i try to run the program.

when I call my print function and put var html into it is is saying unexpected end of input

https://w.trhou.se/k81dhae1ax

I did actually just see what I did wrong. should I still keep the question up even if i figured it out?

Michael Hulet
Michael Hulet
Treehouse Moderator 47,829 Points

Yes, but you should post what you did to fix it as an answer and mark it best so that others know that it's been solved and anyone can learn from it

Steven Parker
Steven Parker
210,475 Points

Or you could just wait for the first person to answer the question and then just mark theirs. :wink:

I would also recommend setting your variables that don't initially hold a value to a blank string. Otherwise you will have an 'undefined' print out to the page. You can simply fix it by changing var html; to var html = '';

The same with your other variables.

One final thing, you may want to swap your array values you are pushing from questionsCorrect.push[i][0] to questionsCorrect.push[0][i]. Otherwise you will only ever print out the first question as correct if the player does get it correct.

4 Answers

Steven Parker
Steven Parker
210,475 Points

It looks like a closing brace is missing at the end of the "printlist" function. It should go on or near line 14.

Your listHTML variable is declared inside of your printlist function but you are updating it and then returning it outside of that function. You may try moving those two lines of code into your function preceding the final curly brace.

Ah I figured it out. You also were missing a closing curly brace after your printlist function which was causing your program to think that the closing curly brace for printlist was the closing bracket for your final for loop. Here's the code with the correction. This works fine. See my previous comment as well though. You will need to move listHTML += '</ul>'; and return(listHTML) inside your printlist function.

//print function
function print(message) {
  document.write(message);
}
function printlist(arr){
// prints the questions you got right
  var listHTML = '<ul>';
  for(i = 0; i < arr.length; i += 1) {
    // creates a list
    listHTML += '<li>' + arr[i] + '</li>';
    listHTML += '</ul>';
    return(listHTML);
  }
}


// 2 demensional array with question, answer combo arrays inside
var questions = [
  ["What programming language is named after an animal?", "python"],
  ["What programming language is named after a stone?", "ruby"],
  ["What programming language is being used to run this program?", "javascript"],
  ["what is another programming language also named after coffee?", "java"]
];
var html;
var answer;
var numberCorrect = 0;
var numberIncorrect = 0;
var questionsCorrect = [];
var questionsIncorrect = [];
// rotates through the questions array and askes them in a pop up box
for (var i = 0; i < questions.length; i += 1) {
  answer = prompt(questions[i][0] + '\n correct answers ' + numberCorrect + '\n incorrect answers ' + numberIncorrect);
  // adds the questions values to a new set of arrays for questions you answered
  // correctley and incorrectly
  if(answer.toLowerCase() === questions[i][1]) {
    numberCorrect += 1;
    questionsCorrect.push(questions[i][0]);
    console.log(questionsCorrect);
  }else{
    numberIncorrect += 1;
    questionsIncorrect.push(questions[i][0]);
    console.log(questionsIncorrect);
  }
  console.log(numberCorrect);
}
html += '<p>total correctly answered: ' + numberCorrect + '<br> total incorrect answers: ' + numberIncorrect + '</p> <br>';
html += '<h2> these are the questions you answered correctly </h2>';
html += printlist(questionsCorrect);
print(html);

Thanks so much for all your help guys. It was originally a closing curly brace on my print list function that was causing the problem. Ben thanks for the information about declaring variables I will keep that in mind when creating new variables! correct me if I am mistaken but if you put the listHTML += '</ul>'; inside the for loop it will print out </ul> between each list item which wont print out correctly. Also when you see undefined print out on the web page it is being caused by the <p> tags at the bottom when I was creating the string. I just removed them and it doesn't appear on the page anymore.

Steven Parker
Steven Parker
210,475 Points

You're exactly right that you only want to add the closing </ul> tag after the loop. And don't forget Michael's suggestion to select a "best answer" so that others know that the question has been solved.

Happy coding! :christmas_tree: And for some holiday-season fun and extra practice, give Advent of Code a try! :santa:

You are exactly right!