<commit_before>// http://stackoverflow.com/a/12646864/92396
function shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}

links = $('#mw-content-text p a').not($('.reference a'))
words = new Set(links.toArray().map(link => link.text))
words = Array.from(new Set(words)); // keep only unique

if (links.length >= 4) { // if less – we don't have enough options to choose for dropdowns
	links.replaceWith(function() {
		var correct = $(this).text();
		var options = [correct];
		while (options.length < 4) {
			word = words[Math.floor(Math.random() * words.length)];
			if (!options.includes(word)) {
				options.push(word);
			}
		}
		options = shuffleArray(options);
		var select = '<select><option>' + options.join('</option><option>') + '</option></select>';
		var item = $(select)
		item.change(function(){
			val = $(this).val()
			if (val === correct) {
				$(this).css('background-color', 'lightgreen');
			}
			else {
				$(this).css('background-color', 'lightcoral');
			}
		});
		item.prop("selectedIndex", -1);
		return item;
	});
}<commit_msg>Add switcher to the top of wiki pages<commit_after>function shuffleArray(array) {
	// http://stackoverflow.com/a/12646864/92396
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    return array;
}

function createQuizzes() {
	links = $('#mw-content-text p a').not($('.reference a'))
	words = new Set(links.toArray().map(link => link.text))
	words = Array.from(new Set(words)); // keep only unique

	if (links.length >= 4) { // if less – we don't have enough options to choose for dropdowns
		links.replaceWith(function() {
			var correct = $(this).text();
			var options = [correct];
			while (options.length < 4) {
				word = words[Math.floor(Math.random() * words.length)];
				if (!options.includes(word)) {
					options.push(word);
				}
			}
			options = shuffleArray(options);
			var select = '<select><option>' + options.join('</option><option>') + '</option></select>';
			var item = $(select)
			item.change(function(){
				val = $(this).val()
				if (val === correct) {
					$(this).css('background-color', 'lightgreen');
				}
				else {
					$(this).css('background-color', 'lightcoral');
				}
			});
			item.prop("selectedIndex", -1);
			return item;
		});
	}
}

// TODO: wait here for jQuery to load

switcher = $('<a>[TEST YOURSELF]</a>').click(createQuizzes).css('color', 'red');
$('#firstHeading').append('&nbsp;').append(switcher);
{'fraction_non_alphanumeric': 0.20139398385913426, 'fraction_numerical': 0.013939838591342627, 'mean_word_length': 3.1761102603369067, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7672511', 'n_tokens_mistral': 1039, 'n_tokens_neox': 909, 'n_words': 214}