Last active
August 2, 2019 02:33
-
-
Save ruliana/189e8ba38dc1c1af146565561393b0f1 to your computer and use it in GitHub Desktop.
Making the "Preposition Game" a little better (still not there)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wordGame(words) { | |
let clicks = 0; | |
let correct = 0; | |
function updateScore() { | |
let score = document.querySelectorAll('.word-game-score-panel')[0]; | |
if (clicks == 0) { | |
score.innerHTML = "The Preposition Game<br>Accuracy: 0%"; | |
} else { | |
let accuracy = Math.round(correct / clicks * 100); | |
score.innerHTML = `The Preposition Game<br>Accuracy: ${accuracy}%`; | |
} | |
} | |
function createScoreArea() { | |
let score = document.createElement("div"); | |
score.classList.add("word-game-score-panel"); | |
score.style.position = "fixed"; | |
score.style.top = "2%"; | |
score.style.right = "2%"; | |
score.style.backgroundColor = "white"; | |
score.style.border = "3px solid red"; | |
score.style.padding = "0.5em"; | |
score.innerHTML = "The Preposition Game<br>Accuracy: 0%"; | |
let body = document.getElementsByTagName("body"); | |
body[0].appendChild(score); | |
} | |
function printSolution(event) { | |
clicks += 1; | |
let element = event.currentTarget; | |
let answer = element.getAttribute("data-answer"); | |
let selectedOptions = Array.from(element.selectedOptions).map(e => e.text); | |
if (selectedOptions.includes(answer)) { | |
correct += 1; | |
element.style.border = "2px solid green"; | |
} else { | |
element.style.border = "2px solid red"; | |
} | |
updateScore(); | |
} | |
function toRegexp(wordArray) { | |
let expr = wordArray.join("|"); | |
return new RegExp(`\\b(${expr})\\b`, 'g'); | |
} | |
function toOptions(wordArray) { | |
let words = [""].concat(wordArray); | |
return words.map(w => `<option>${w}</option>`) | |
.join(""); | |
} | |
function initGame() { | |
theWordGameIsRunning = true; | |
createScoreArea(); | |
let regexp = toRegexp(words); | |
let prepOptions = toOptions(words); | |
let prepSelect = `<select class="word-game" data-answer="$1" style="border: 2px solid black">${prepOptions}</select>`; | |
let paragraphs = document.getElementsByTagName("p"); | |
for (let p of paragraphs) { | |
p.innerHTML = p.innerHTML | |
.replace(regexp, prepSelect); | |
let selections = p.querySelectorAll('.word-game'); | |
for (let e of selections) { | |
e.addEventListener('change', printSolution); | |
} | |
} | |
} | |
function resetGame() { | |
for (e of document.querySelectorAll('.word-game')) { | |
e.selectedIndex = 0; | |
e.style.border = "2px solid black"; | |
} | |
clicks = 0; | |
correct = 0; | |
updateScore(); | |
} | |
let selections = document.querySelectorAll('.word-game'); | |
if (selections.length > 0) { | |
resetGame(); | |
} else { | |
initGame(); | |
} | |
} | |
wordGame(["on", "at", "in"]); | |
wordGame(["for", "to", "by"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment