Skip to content

Instantly share code, notes, and snippets.

@saminwankwo
Last active September 16, 2019 14:09
Show Gist options
  • Save saminwankwo/857b7c616ba963264db2c8041ac49049 to your computer and use it in GitHub Desktop.
Save saminwankwo/857b7c616ba963264db2c8041ac49049 to your computer and use it in GitHub Desktop.
A simple quiz application designed in javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>var questions = [
['How many States are in Nigeria?', '36'],
['How many continents are there?', '7'],
['How many legs does an insect have?', '6'],
];
var correctAnswers = 0;
var question;
var answer;
var response;
var correct = [];
var wrong = [];
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
function buildList(arr) {
listHtml = '<ol>';
for (var i = 0; 1 < arr.length; i += 1){
listHtml += '<li>' + arr[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) {
correctAnswers += 1;
correct.push(question);
} else {
wrong.push(question);
}
}
html = 'You got ' + correctAnswers + ' question(s) right';
html += 'You got these questions correct';
html += buildList(correct);
html += 'You got these questions wrong';
html += buildList(wrong);
print(html)
</script>
<div id="output">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment