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 trialAhmed Mohamed Fouad
11,735 Pointsanother solution for "quiz challenge part 2"
in quiz.js
var questions = [
['How many states are in the United States?', 50],
['How many continents are there?', 7],
['How many legs does an insect have?', 6]
];
var correct = [];
var wrong = [];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function printcorrect(message) {
var outputDiv = document.getElementById('correct');
outputDiv.innerHTML = message;
}
function printwrong(message) {
var outputDiv = document.getElementById('wrong');
outputDiv.innerHTML = message;
}
function buildlist(message) {
var arr = [];
for (i = 0; i < message.length; i++) {
arr += message[i];
}
return arr;
}
for (var i = 0; i < questions.length; i += 1) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
response = parseInt(response);
if (response === answer) {
correctAnswers += 1;
correct.push("<li>" + questions[i][0] + "</li>");
} else {
wrong.push("<li>" + questions[i][0] + "</li>");
}
}
html = "You got " + correctAnswers + " question(s) right."
print(html);
if (correctAnswers !== 0)
printcorrect(buildlist(correct));
if (correctAnswers !== 3)
printwrong(buildlist(wrong));
in index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Quiz</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>JavaScript Quiz</h1>
<div id="output">
</div>
<h2>you got those questions correct:</h2>
<ol id="correct">
</ol>
<h2>you got those questions wrong:</h2>
<ol id="wrong">
</ol>
<script src="js/quiz.js"></script>
</body>
</html>
what do you think ?