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

Thoughts? I am sure this could be more efficient, but I tried to add safeguards for number/letters answers.

var quiz = [
 ["What color is a fire truck?", "red"],
 ["How many legs does a cat have?", "4"],
 ["What is 1 + 2?", "3"] 
]

var rightQuestions = [];
var wrongQuestions= [];
var score = 0;
var wrong = 0;
var newAnswer= "";
var htmlMessage = "";
var wrongOnes = "";
var rightOnes = "";
var wrongOnesHTML = "";
var rightOnesHTML = "";



function print(message) {
  document.write(message);
}


function ask(question){
 var  answer = prompt(question); 
 return answer;
}

//Goes through array to create questions for test
function buildList(questions){
 var list = "";
 list += "<ul>";
 for (i=0; i < questions.length; i+=1) {
  list += "<li> " + questions[i] + "</li>";    
 }
  list += "</ul>";
  return list;
}

//Allows user to enter "three" and "four" for "3" and "4" and still get correct
function cleanUpAnswer (item){ 
  if (item === "three"){
    item = "3";
  }else if(item === "four"){
      item = "4";
  }
    return item;
}


// Take The Test; Determine what to do with right and wrong questions

for (i=0; i<quiz.length; i+=1) {
 answer = ask(quiz[i][0]);
 console.log(answer);
   newAnswer = cleanUpAnswer(answer.toLowerCase());
   console.log(newAnswer);
 if (newAnswer === quiz[i][1]){ 
   console.log ("Correct!");
   score +=1; 
   rightQuestions.push(quiz[i][0]);
   console.log(rightQuestions); 
 }else {
    console.log ("NOT Correct!");
    wrongQuestions.push(quiz[i][0]);
    console.log(wrongQuestions); 
 }

}
console.log(score);
wrong = wrongQuestions.length;
//Create the Final Score and Lists
htmlMessage = "You got " + score + " correct, and " + wrong + "  wrong!";
wrongOnes = buildList(wrongQuestions); 
rightOnes = buildList(rightQuestions);
wrongOnesHTML = "<h1>You Got These Questions Wrong</h1>";
rightOnesHTML = "<h1>You Got These Questions Right</h1>";


// Print Out the Final Score and Lists
print(htmlMessage);

//Don't show right or wrong questions unless there is something in array
if (wrongQuestions.length > 0){
print(wrongOnesHTML + wrongOnes);
}
if (rightQuestions.length > 0){
print(rightOnesHTML + rightOnes);
}