quiz project in javascript code example

Example 1: how to create a quiz in p5

var myQuestions = [
	{
		question: "What is 10/2?",
		answers: {
			a: '3',
			b: '5',
			c: '115'
		},
		correctAnswer: 'b'
	},
	{
		question: "What is 30/3?",
		answers: {
			a: '3',
			b: '5',
			c: '10'
		},
		correctAnswer: 'c'
	}
];

Example 2: how to create a quiz in p5

function generateQuiz(questions, quizContainer, resultsContainer, submitButton){

	function showQuestions(questions, quizContainer){
		// code will go here
	}

	function showResults(questions, quizContainer, resultsContainer){
		// code will go here
	}

	// show the questions
	showQuestions(questions, quizContainer);

	// when user clicks submit, show results
	submitButton.onclick = function(){
		showResults(questions, quizContainer, resultsContainer);
	}
}

Example 3: quiz javascript example with array

var questions = [
["What is the capital of England?", "LONDON", "You know, where the queen lives"],
["What is the capital of France?", "PARIS", "Remember the eiffel tower?"],
["What is the capital of Canada?", "OTTAWA", "Lot of mooses there"]
];

var correctAnswers = 0;

for (var i = 0; i < questions.length; i++) {
    var answer = prompt(questions[i][0]);
    if (answer.toUpperCase() == questions[i][1]) {
        alert("Correct! " + questions[i][2]);
        correctAnswers++;
    }
    else {
        alert("incorrect, the correct answer was " + questions[i][1]);
    }
}

Tags:

Html Example