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 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!