Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Ruby Bassi
Ruby Bassi
3,873 Points

Code works but what if I wanted to ask a mixture of string and integer based answers?

Hi my lovelies, I got my code to work based on string responses, and added the .toLowerCase() method.

What if I want to change my last question array to: 'how many children did Abraham have?', '2'. Where would I use parseInt() (to convert the response to an integer) in my code?

Appreciate your time! Ruby x

var correctCounter =0;
var wrongCounter =0;
var answers;
var QandAList = [
['Name the film with the boy, red balloon and clown', 'it'],
['which country was the famous poet, Hafez, born?', 'iran'],
['who was god of the sea?', 'poseidon'],
];

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

for ( var i = 0; i < QandAList.length; i++) {
  answers = prompt(QandAList[i][0]);
  answers = answers.toLowerCase();
  if (answers === QandAList[i][1]) {
   correctCounter +=1; 
   alert('Sweet! ' + answers  + ' is correct');
} else {
   wrongCounter +=1;
   alert('Oops! ' + answers + ' is incorrect');
} 
}
print('You got ' + correctCounter + ' answers correct and ' + wrongCounter + ' incorrect. Thanks for playing. ');

1 Answer

Hey there! With this code, I think an option is to simply put the number in quotations.

For example: ['Name the film with the boy, red balloon and clown', '1']

Good luck!