FANDOM


The Quiz script allows you to add single choice quizzes on your wiki.

Options

Changing the quiz name or language is optional (will default to English), but for the quiz to run correctly, you need to define a set of questions and answers as well as the text which will be displayed once the user completes the quiz.

Make sure the options are defined BEFORE the script import.

Using configuration options with Fandom Open Source Library scripts
The instructions on this page describe how to use configuration options with a script. Here on the Fandom Open Source Library, many scripts provide optional configuration settings as a mean to alter or enhance the default behavior of the script. When installing configuration options in your JavaScript file, please note that they need to go above the import statement in order to work — unless the directions say otherwise. In case MediaWiki:ImportJS is used to load the scripts, it will be executed last.
Configuration options load too late, don't work
Proper placement of configuration options
// 1. AjaxRC import statement
importScriptPage('AjaxRC/code.js','dev');
 
// 2. AjaxRC configuration option
var ajaxRefresh = 30000;
// 1. AjaxRC configuration option
var ajaxRefresh = 30000;
 
// 2. AjaxRC import statement
importScriptPage('AjaxRC/code.js','dev');


Quiz name

var quizName = "Quiz";

Replace Quiz, if you wish to have a customized name for the quiz.

Language

var quizLang = "en";

Replace en with another supported language code. If a given language is not supported, the corresponding message will default to English. If you wish to submit translations, please add them to the talk page or update the code yourself (after making sure you know what you're doing).

Results

var resultsTextArray = [
		RESULTS_GO_HERE
	];

This variable lets you define a set of potential result messages, displayed after the user finishes the quiz, which will depend on the user's score. The first provided message will be the result that is displayed after the user gets the lowest score. The last provided message will be displayed when he/she gets the top score. You can add as many results as you wish, separated by commas. Remember NOT TO add a comma after the last message in the array.

Questions and answers

var questions = [
		QUESTIONS_AND_ANSWERS_GO_HERE
	];

This is the most important variable. Each set of questions and answers needs to be delimited by square brackets and each item in every set needs to end with a comma. The first item in each set must be the question. The second item must be the correct answer. Don't worry, the answers will be displayed in a random order. Any other item in a given set should be false answers. Remember not to add a comma after every last item in every set as well as after the last set. If in doubt, see the generic, example code below.

Installation

Add this JavaScript on your wiki:

Site-wide
MediaWiki:Common.js
importArticles({
    type: "script",
    articles: [
        'u:dev:Quiz/code.js'
    ]
});
importArticles — Best Practices for installing JavaScript on FANDOM
The importArticles statement is designed to combine multiple HTTP requests into a single data transfer, allowing multiple scripts to load and execute faster. If you've been installing several different scripts, your JavaScript file has probably accumulated unnecessary import statements. Click "Expand" to learn how to efficiently batch import scripts to speed up performance and make your code look cleaner. One other approach is by using the MediaWiki:ImportJS.
If your JavaScript file has several lines of code that say importScript, importScriptPage, or importArticles, you may be able to combine them! By batch importing a collection of scripts with a single import, your JavaScript code will load faster and look cleaner. Consider the example below. On the left is an example of what your JavaScript file might currently look like. On the right is how you could improve that code.
Multiple imports — messy and slow One import — clean and efficient
importScriptPage('AjaxRC/code.js','dev');
 
importScript('MediaWiki:localScript.js');
 
importArticle({
  type: 'script',
  article: 'u:dev:FloatingToc/code.js'
});
 
importScriptPage('page1.js', 'wikiname');
 
importScriptPage('page2.js', 'wikiname');
importArticles({
    type: 'script',
    articles: [
        'u:dev:AjaxRC/code.js',
        'MediaWiki:localScript.js',
        'u:dev:FloatingToc/code.js',
        'u:wikiname:page1.js',
        'u:wikiname:page2.js'
    ]
});
Note: In this example, pay close attention to the placement of commas and other punctuation. For people who aren't familiar with programming (and even those who are!), a common mistake when writing code is to accidentally delete, forget, or misplace critical symbols like commas or quotation marks. This can cause a syntax error that breaks the code. Carefully follow the convention shown here when using importArticles.
But there's much more to importArticles than just this! For more examples and advanced usage, see the help page at Help:Including additional JavaScript and CSS.


To add the quiz you will need to import the script to MediaWiki:Common.js on your wiki. You will also need to define questions and the text which will be displayed depending on the score the users get. You can view a demo here and the code required for it to run here. On the bottom of this page is an example, complete script to paste on your wiki, ready to adjust.

The second step is to add

<div id="quizQ"></div>

on the page you want to put the quiz.


Code to copy/paste

var quizName = "Quiz";
var quizLang = "en";
var resultsTextArray = [ 
	"Text displayed after the user completes the quiz with the LOWEST score",
	"Text displayed after the user completes the quiz somewhere between the lowest and highest scores. Feel free to add more than one of these (separated by commas and inside double quotes)",
	"Text displayed after the user completes the quiz with the HIGHEST score" 
	];
var questions = [
 
	["This is the first question",
	"The CORRECT answer to question 1",
	"An INCORRECT answer to question 1",
	"Another INCORRECT answer to question 1",
	"Yet Another INCORRECT answer to question 1"], 
 
	["This is the second question, feel free to add as many questions as you like",
	"The CORRECT answer to question 2",
	"An INCORRECT answer to question 2",
	"Another INCORRECT answer to question 2"],
 
	["This is the third question",
	"The CORRECT answer to question 3",
	"An INCORRECT answer to question 3"]
 
	];
 
importArticles({
    type: 'script',
    articles: [
        'u:dev:Quiz/code.js'
    ]
});

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.