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

On quiz, All works except doesn't display list of question got it right and got it wrong. Can you debug it? thx

var questions = [ ['Have I been to Turkey,?', 'yes'], ['Which Sport is my Favorite Sport Tennis or Rugby?', 'tennis'], ['Is Aquire my favorite board game,?', 'yes'], ['Do I live in South Seattle?', 'yes'] ];

var correctGuess=0; // counter variable var question; var answer; var response; var results; var correct = []; // empty array to include list questions got right var wrong = []; // empty array to include list questions got wrong

for (var i = 0; i < questions.length; i++) { question= questions[i][0];// accessing the question/1st index i.e 0 answer=questions [i][1]; //accessing the answer/2nd index i.e 1

response=(prompt(question)).toLowerCase(); if (response===answer) { alert('You got it right'); correctGuess+= 1; correct.push (question); } else { alert('You got it wrong'); wrong.push (question); } }

function displayResults (message) { var printHTML= document.getElementById('outcome'); printHTML.innerHTML=message; }

var results= '<p>You got ' + correctGuess + ' question(s) right'; if (correctGuess >= 4) { results += "<strong> and You Earned a Gold Medal</strong></p>"; displayResults(results); } else if (correctGuess >= 3) { results += "<strong> and You Earned a Silver Medal</strong></p>"; displayResults(results); } else if (correctGuess >= 2) { results += "<strong> and You Earned a Bronze Medal</strong></p>"; displayResults(results); } else { results += "<strong> and You Earned No Medal</strong></p>";

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

listHTML+='</ol>'; return listHTML; results += '<h2>You got these questions correct:</h2>'; results += buildList(correct); results += '<h2>You got these questions wrong:</h2>'; results += buildList(wrong); displayResults(results); }

var userInput= prompt("Welcome to my App, What is Your Name?"); var welcome= alert ('Hello ' + userInput + ', let\'s play guessing game, if you guess correctly you will get a medal');

//Two dimensional array with text question and answer var questions = [ ['Have I been to Turkey,?', 'yes'], ['Which Sport is my Favorite Sport Tennis or Rugby?', 'tennis'], ['Is Aquire my favorite board game,?', 'yes'], ['Do I live in South Seattle?', 'yes'] ];

var correctGuess=0; // counter variable var question; var answer; var response; var results; var correct = []; // empty array to include list questions got right var wrong = []; // empty array to include list questions got wrong

for (var i = 0; i < questions.length; i++) { question= questions[i][0];// accessing the question/1st index i.e 0 answer=questions [i][1]; //accessing the answer/2nd index i.e 1

response=(prompt(question)).toLowerCase(); if (response===answer) { alert('You got it right'); correctGuess+= 1; correct.push (question); } else { alert('You got it wrong'); wrong.push (question); } }

function displayResults (message) { var printHTML= document.getElementById('outcome'); printHTML.innerHTML=message; }

var results= '<p>You got ' + correctGuess + ' question(s) right'; if (correctGuess >= 4) { results += "<strong> and You Earned a Gold Medal</strong></p>"; displayResults(results); } else if (correctGuess >= 3) { results += "<strong> and You Earned a Silver Medal</strong></p>"; displayResults(results); } else if (correctGuess >= 2) { results += "<strong> and You Earned a Bronze Medal</strong></p>"; displayResults(results); } else { results += "<strong> and You Earned No Medal</strong></p>";

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

listHTML+='</ol>'; return listHTML; results += '<h2>You got these questions correct:</h2>'; results += buildList(correct); results += '<h2>You got these questions wrong:</h2>'; results += buildList(wrong); displayResults(results); }

1 Answer

Steven Parker
Steven Parker
243,228 Points

Take a look at the last dozen or so lines of your code.

:point_right: It looks like you put a close brace (}) in the wrong place:

function buildList(arr) {
  var listHTML = "<ol>";
  for (var i = 0; i < arr.length; i += 1) {
    listHTML += '<li>' + arr[i] + '</li>';
  }
  listHTML += '</ol>';
  return listHTML;
                                                 // <-- you probably meant to put the brace here
  results += '<h2>You got these questions correct:</h2>';
  results += buildList(correct);
  results += '<h2>You got these questions wrong:</h2>';
  results += buildList(wrong);
  displayResults(results);
}                                                // <-- current position of brace

Also, I notice you called displayResults several times earlier in the code, but the results they send to the document will be overwritten when this last one runs.

Steve,thanks for catching that error. i fixed it it,but it still not displaying correct and wrong questions on the page. It was pointing to the last 7th line from bottom //return listHTML; // as outside of function. i commented it out, and also tried putting inside of function ,it i didn't work. return listHTML; var questions = [ ['Have I been to Turkey,?', 'yes'], ['Which Sport is my Favorite Sport Tennis or Rugby?', 'tennis'], ['Is Aquire my favorite board game,?', 'yes'], ['Do I live in South Seattle?', 'yes'] ];

var correctGuess=0; // counter variable var question; var answer; var response; var results; var correct = []; // empty array to include list questions got right var wrong = []; // empty array to include list questions got wrong

for (var i = 0; i < questions.length; i++) { question= questions[i][0];// accessing the question/1st index i.e 0 answer=questions [i][1]; //accessing the answer/2nd index i.e 1

response=(prompt(question)).toLowerCase(); if (response===answer) { alert('You got it right'); correctGuess+= 1; correct.push (question); } else { alert('You got it wrong'); wrong.push (question); } }

function displayResults (message) { var printHTML= document.getElementById('outcome'); printHTML.innerHTML=message; }

var results= '<p>You got ' + correctGuess + ' question(s) right'; if (correctGuess >= 4) { results += "<strong> and You Earned a Gold Medal</strong></p>"; displayResults(results); } else if (correctGuess >= 3) { results += "<strong> and You Earned a Silver Medal</strong></p>"; displayResults(results); } else if (correctGuess >= 2) { results += "<strong> and You Earned a Bronze Medal</strong></p>"; displayResults(results); } else { results += "<strong> and You Earned No Medal</strong></p>";

} function buildList (arr) { var listHTML="<ol>"; for (var i=0; i <arr.length; i+=1) { listHTML+= '<li>' + arr[i] + '</li>'; } } listHTML+='</ol>'; //return listHTML; results += '<h2>You got these questions correct:</h2>'; results += buildList(correct); results += '<h2>You got these questions wrong:</h2>'; results += buildList(wrong); displayResults(results);

also, can you tell me how to post codes here without wrapping like you did here