JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Stacy Loggins
Stacy Loggins
2,940 Points

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

Stacy Loggins
Stacy Loggins
2,940 Points

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 46,956 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
172,268 Points

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

benyoungblood
benyoungblood
6,656 Points

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
172,268 Points

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

benyoungblood
benyoungblood
6,656 Points

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.

benyoungblood
benyoungblood
6,656 Points

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);
Stacy Loggins
Stacy Loggins
2,940 Points

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
172,268 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:

benyoungblood
benyoungblood
6,656 Points

You are exactly right!