## 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. # My Solution to Build a Quiz Challenge Part 1

Here is my solution to this challenge: https://w.trhou.se/zkpp6wbqjx

Here's the JavaScript:

```var numberCorrect = 0;
var qAndA = [
['What is 2 * 3?', '6'],
['What is 49 / 7?', '7'],
['What is 7 + 5?', '12']
];

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

for ( var i = 0; i < qAndA.length; i++){
var userAnswer = prompt( qAndA[i] );
if ( userAnswer === qAndA[i] ){
numberCorrect++;
}
}

print('You answered ' + numberCorrect + ' out of ' + qAndA.length + ' questions correctly.');
```

Let me know if you had any comments or improvements. Thank you. Here's my solution!

```var correctQuestions = "";
var incorrectQuestions = "";

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

var questions = [
['How many states are there?', '50'],
['How old am I?', '27'],
['What year was I born?', '1991']
];

for ( i = 0; i < questions.length; i++ ) {
if ( answer === questions[i] ) {
correctQuestions += '<p>' + questions[i] + '</p>';
} else {
incorrectQuestions += '<p>' + questions[i] + '</p>';
}
}

print('<h2>You got these questions right:</h2>' + correctQuestions + '<h2>You got these questions wrong:</h2>' + incorrectQuestions);
```

another version with more conditions

```// instantiate global variables (counters)
var qCounter = 0;
var numRight = 0;
var numWrong = 0;

//create arrays for data
var questionPool = [
['158=___ + 106. What number will come in the blank to make the number sentence true?', '52'],
['Which moon is larger than Mercury?', 'io'],
['What is the name of the little dragon in the animated movie Mulan?', 'mushu'],
['What is the name of the prison in the film The Rock?', 'alcatraz'],
['Give another name for the study of fossils?', 'paleontology'],
['Which insects cannot fly, but can jump higher than 30 cm?', 'flea'],
['Which plant does the Canadian flag contain?', 'maple'],
['Which is the largest desert on earth?', 'sahara'],
['What is the largest state of the United States?', 'alaska'],
['What is the capital of the American state Hawaii?', 'honolulu'],
];

// functions to be used
// updated print function to html doc
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
//calculate percentage
function calcPercent() {
var total = numRight + numWrong;
return numRight / total * 100;
}
//gather questions and put in html list format
function listQuestions(arr) {
var html = '<ol>';
for (var i = 0; i < arr.length; i++) {
html += '<li>' + arr[i] + '</li>';
}
html += '</ol>';
return html;
}

//main program. Iterate through array. Bring answer to lower case. Count right/wrong.Push to appropriate arrays
for (var i = 0; i < questionPool.length; i++) {
var question = questionPool[i];
numRight++;
} else {
numWrong++;
}
}
// testing values. Note - only for dev testing
console.log('Testing Number right - '+numRight);
console.log('Testing Number wrong - '+numWrong);

//print html will depend on conditions
html = '<p>Here is your results:</p>';
if (numRight > 1 && numRight <10){
html += 'You got ' + numRight + ' correct questions and ' + numWrong + '  wrong questions. '+ calcPercent() + '%';
html += '<h2> Here is a list of questions you got right';
html += '<h2> Here is a list of questions you got wrong';
} else if (numRight == 1){
html += 'You only got ' + numRight + ' correct question and ' + numWrong + ' wrong. ' + calcPercent() + '%';
html += '<h2> Here is the question you got right';
html += '<h2> Here is a list of questions you got wrong';
} else if(numRight == 0) {
html += 'You did not get any correct questions. Please try again next time. '+ calcPercent() + '%';
html += '<h2> Here is a list of all the questions you got wrong';
} else {
html += 'You got all ' + numRight + ' questions correct. Good Job! '+ calcPercent() + '%';
html += '<h2> Here is a list of all the questions you got right';
}
//doc write to div built html variable
print(html);
``` Here is my solution that handles string vs number:

```var questions= [
['Who teaches this class?', 'dave mcfarland'],
['What is the meaning of life?', 42]
]

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

var correctQuestions = [];

var incorrectQuestions = [];

function evaluator(questionArray) {
var html= '<ol>';
for(var i = 0; i < questionArray.length; i += 1) {
html += '<li>' + questionArray[i] + '</li>';
}
html += '</ol>';
print(html);
}

function quiz(questions) {
for(var i = 0; i < questions.length; i += 1){
} else {
}
correctQuestions.push(questions[i]);
} else{
incorrectQuestions.push(questions[i]);
}
}

print('<p>You got ' + correctAnswerCounter + ' question(s) right.</p>');

print('<h3>You got these questions correct:</h3>');
evaluator(correctQuestions);

print('<h3>You got these questions wrong:</h3>');
evaluator(incorrectQuestions);
}

quiz(questions);
```

my solution

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

//initial dimentional array questions with answers
var quizQuestions = [
['How many states are in the U.S.?', '50'],
['How many legs does an insect have?', '6'],
['How many continents are there?', '7']
];

// need to make array collectors for right/wrong answers
var rightQ = [];
var wrongQ = [];

var rightA = 0;
var wrongA = 0;

// iterate through dimentional array and push to collector array as well as add to counters
for (var i=0;i < quizQuestions.length; i++) {
if (answer === quizQuestions[i] ) {
rightA++;
rightQ.push(quizQuestions[i]);
}else {
wrongA++;
wrongQ.push(quizQuestions[i]);
}

}

//function to collect array values and build html list to print
function collectQ(arrayQ){
html = '<ol>';
for (var i=0; i < arrayQ.length; i++){
html+= '<li>' + arrayQ[i] + '</li>';
}
html += '</ol>';
print(html);
}

//print titles with final values using array data and function output
print('You got ' + rightA + ' question(s) right.');

print('<h2 style="font-weight: bold"> You got these question(s) right.</h2>');
collectQ(rightQ);

print('<h2 style="font-weight: bold"> You Got these question(s) wrong.</h2>');
collectQ(wrongQ);
```

Sorry for the weird code post - I followed the markdown for posting code and I got this

```// instantiate global variables (counters)
var qCounter = 0;
var numRight = 0;
var numWrong = 0;

//create arrays for data
var questionPool = [
['158=___ + 106. What number will come in the blank to make the number sentence true?', '52'],
['Which moon is larger than Mercury?', 'io'],
['What is the name of the little dragon in the animated movie Mulan?', 'mushu'],
['What is the name of the prison in the film The Rock?', 'alcatraz'],
['Give another name for the study of fossils?', 'paleontology'],
['Which insects cannot fly, but can jump higher than 30 cm?', 'flea'],
['Which plant does the Canadian flag contain?', 'maple'],
['Which is the largest desert on earth?', 'sahara'],
['What is the largest state of the United States?', 'alaska'],
['What is the capital of the American state Hawaii?', 'honolulu'],
];

// functions to be used
// updated print function to html doc
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
//calculate percentage
function calcPercent() {
var total = numRight + numWrong;
return numRight / total * 100;
}
//gather questions and put in html list format
function listQuestions(arr) {
var html = '<ol>';
for (var i = 0; i < arr.length; i++) {
html += '<li>' + arr[i] + '</li>';
}
html += '</ol>';
return html;
}

//main program. Iterate through array. Bring answer to lower case. Count right/wrong.Push to appropriate arrays
for (var i = 0; i < questionPool.length; i++) {
var question = questionPool[i];
numRight++;
} else {
numWrong++;
}
}
// testing values. Note - only for dev testing
console.log('Testing Number right - '+numRight);
console.log('Testing Number wrong - '+numWrong);

//print html will depend on conditions
html = '<p>Here is your results:</p>';
if (numRight > 1 && numRight <10){
html += 'You got ' + numRight + ' correct questions and ' + numWrong + '  wrong questions. '+ calcPercent() + '%';
html += '<h2> Here is a list of questions you got right';
html += '<h2> Here is a list of questions you got wrong';
} else if (numRight == 1){
html += 'You only got ' + numRight + ' correct question and ' + numWrong + ' wrong. ' + calcPercent() + '%';
html += '<h2> Here is the question you got right';
html += '<h2> Here is a list of questions you got wrong';
} else if(numRight == 0) {
html += 'You did not get any correct questions. Please try again next time. '+ calcPercent() + '%';
html += '<h2> Here is a list of all the questions you got wrong';
} else {
html += 'You got all ' + numRight + ' questions correct. Good Job! '+ calcPercent() + '%';
html += '<h2> Here is a list of all the questions you got right';
}
//doc write to div built html variable
print(html);
``` Got the code to work fine but HTML formatting I have no idea what im doing, hoping at some point this fullstackJS course is going to cover html and css.

```function print(message) {
document.write(message);
}
var html = "<ol>";
var incorrectQ = [];
var correctQ = [];
var corAns = 0;
var questions=[
["which programming lanugage are we using?", "JavaScript"],
["which programming language is named after a snake?", "Python"],
["which programming language is named after a gemstone", "Ruby"]
]
for(i=0;i<questions.length;i+=1){
var ques = prompt(questions[i]);
if(ques.toLowerCase() === (questions[i]).toLowerCase()){
correctQ.push(questions[i]);
corAns++
}else if(ques.toLowerCase() !== (questions[i]).toLowerCase()){
incorrectQ.push(questions[i]);
}
}
print(`you got \${corAns} questions correct and \${questions.length - corAns} incorrect.<br>`);
print("<h1>you got the following questins correct</h1>"+ "<li>"+ correctQ.join(", ")+"</li>" + "</ol>");
print("<h1>you got the following questins incorrect</h1>" + incorrectQ.join(", ") + "</ol>");
```