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

Brian Prouty
seal-mask
.a{fill-rule:evenodd;}techdegree
Brian Prouty
Front End Web Development Techdegree Student 10,143 Points

.push() wont grab correct variable

For whatever reason I cannot get my program to pick up the question variable for this code challenge. I changed the .push method to say

questionsCorrect.push(questions[i]);

to get the code to work how I want it to for the challenge, but in the solution video it says to just write it how it is shown below... But when I do that it just prints the letter H for everything.

My code is as shown below:

JavaScript
var questions = [
  ['How many states are in the United States?', 50],
  ['How many continents are there?', 7],
  ['How many legs does an insect have?', 6]
];
var correctAnswers = 0;
var wrongAnswers = 0;
var question;
var answer;
var response;
var html;

var questionsCorrect = [];
var questionsWrong = [];

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function printList( list ) {
  var listHTML = '<ol>';
  for ( var i = 0; i < list.length; i += 1) {
    listHTML += '<li>' + list[i][0]  + '</li>';
  }
  listHTML += '</ol>';
  return listHTML;
}

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);
  response = parseInt(response);
  if (response === answer) {
    correctAnswers += 1;
    questionsCorrect.push(question);
  } else {
    wrongAnswers += 1;
    questionsWrong.push(question);
  }
}

html = "<h2>You got " + correctAnswers + " question(s) right.</h2>"
html += "They were: " + printList(questionsCorrect);
html += "<h2>You got " + wrongAnswers + " question(s) wrong.</h2>"
html += "They were: " + printList(questionsWrong);
print(html);

1 Answer

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,723 Points

In printList you are using list[i][0] and that is grabbing the first letter. Use list[i] to grab the whole sentence.

Brian Prouty
seal-mask
.a{fill-rule:evenodd;}techdegree
Brian Prouty
Front End Web Development Techdegree Student 10,143 Points

Thanks Clayton Perszyk for whatever reason I was in the mindset that I had to put the second index value to grab the first index of the list but it makes sense since i am already grabbing the first string by calling question!