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

Faisal Rahimi
Faisal Rahimi
4,019 Points

Help on - TWO DIMENTIONAL ARRAYS- please

Please see my code, problem i'm facing is that in the question and answers I have numbers and one string answer, when all questions answered correctly the programm shows the string answered question as incorrect and when i removed the parseInt() METHOD then the programm won't recognise the number answers only the string.

var Questions = [
  [
    "How many kids you have",
    2
  ],
  [
    "Flat number",
    20
  ],
  [
    "What is your name?",
    "Faisal"
  ]

];
var html = '';
var question;
var answer;
var response;
var correct = [];
var wronge = [];
var rightanswer = 0;
var wronganswer = 0;



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

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

for(var i =0; i< Questions.length; i +=1){
question = Questions[i][0];
answer = Questions[i][1];
response = parseInt(prompt(question));
  if(response === answer){
 rightanswer+=1;
    correct.push(question + ". <h2> answer was  = "+ answer + "</h2>");
  }
else{
wronganswer+=1;
  wronge.push(question + ". Correct answer was = " + answer);
}
}

html ="You have correctly answered " + rightanswer + " questions correctly";
html+= "<h2>Questions answered correctly are: </h2>";
html+=buildlist(correct);

html+="You have incorrectly answered " + wronganswer + " questions";
html+= "<h2>Questions answered incorrectly are: </h2>";
html+=buildlist(wronge);

print(html);

4 Answers

Steven Parker
Steven Parker
229,644 Points

It sounds like you were close to discovering the answer yourself.

Your observations about parseInt nearly did it. The issue is that you can't use the same technique to test a numeric answer as you do to test a string answer. You'll either need to handle the answers differently based on type, or make all answers be the same type.

The easiest way to resolve this issue might be to treat all answers as strings. Just remove the call to parseInt, and then put quotes around the first two answers in the Questions array.

Faisal Rahimi
Faisal Rahimi
4,019 Points

Great answer sir, by removing the parseInt() method and adding quote marks on number and string answers programms works. I even tried it with an ARRAY OF OBJECTS, same way same result. By the way you have a very cool name (Steven Parker) cheers for that.

Steven Parker
Steven Parker
229,644 Points

Thank you, I've never heard that before. In my country it is considered a rather plain name.

Faisal Rahimi
Faisal Rahimi
4,019 Points

You must have heard of Peter Parker --(SPIDER-MAN).HA

Steven Parker
Steven Parker
229,644 Points

Sure, but we're not related. :wink: