|
| 1 | +// Functions |
| 2 | + |
| 3 | +function updateOptionLanguage(id, textCont, value) { |
| 4 | + document.getElementById(id).textContent = textCont; |
| 5 | + document.getElementById("select").querySelector(`option[value="${value}"]`).textContent = textCont; |
| 6 | +}; |
| 7 | + |
| 8 | +function updateLanguage(language) { |
| 9 | + const t = translations[language]; |
| 10 | + document.title = translations[language].title; |
| 11 | + document.getElementById("inputField").placeholder = t.placeholder; |
| 12 | + document.getElementById("checkButton").textContent = t.checkButton; |
| 13 | + document.getElementById("skipButton").textContent = t.skipButton; |
| 14 | + document.getElementById("skippedExamples").textContent = t.skippedExamples; |
| 15 | + document.getElementById("solvedExamples").textContent = t.solvedExamples; |
| 16 | + document.getElementById("mistakes").textContent = t.mistakes; |
| 17 | + updateOptionLanguage("add10", t.add10, "0"); |
| 18 | + updateOptionLanguage("add100", t.add100, "1"); |
| 19 | + updateOptionLanguage("add1000", t.add1000, "2"); |
| 20 | + updateOptionLanguage("subtraction10", t.subtraction10, "3"); |
| 21 | + updateOptionLanguage("subtraction100", t.subtraction100, "4"); |
| 22 | + updateOptionLanguage("subtraction1000", t.subtraction1000, "5"); |
| 23 | + updateOptionLanguage("multiplication10", t.multiplication10, "6"); |
| 24 | + updateOptionLanguage("division10", t.division10, "7"); |
| 25 | + document.getElementById("correctly").textContent = t.correctly; |
| 26 | + document.getElementById("wrongly").textContent = t.wrongly; |
| 27 | + document.getElementById("correctAnswerP").textContent = t.correctAnswerP; |
| 28 | +} |
| 29 | + |
| 30 | +function openSidebar() { |
| 31 | + document.getElementById("sidebar").classList.toggle("open"); |
| 32 | + document.getElementById("overlay").classList.toggle("open"); |
| 33 | +}; |
| 34 | + |
| 35 | +function closeSidebar() { |
| 36 | + document.getElementById("sidebar").classList.remove("open"); |
| 37 | + document.getElementById("overlay").classList.remove("open"); |
| 38 | +}; |
| 39 | + |
| 40 | +function generateExample() { |
| 41 | + a = Math.floor((Math.random() * 90 + 10) / variable); |
| 42 | + b = Math.floor((Math.random() * 90 + 10) / variable); |
| 43 | + |
| 44 | + if (sign === "-") { |
| 45 | + if (a < b) { |
| 46 | + [a, b] = [b, a]; |
| 47 | + }; |
| 48 | + } else if (sign === "/") { |
| 49 | + a = a * b; |
| 50 | + }; |
| 51 | +}; |
| 52 | + |
| 53 | +function resetCounters() { |
| 54 | + skipped = 0; |
| 55 | + solved = 0; |
| 56 | + mistakes = 0; |
| 57 | + document.getElementById("example").textContent = `${a}${sign}${b}=`; |
| 58 | + document.getElementById("inputField").value = ""; |
| 59 | + document.getElementById("skippedExamples").textContent = `${skippedText} ${skipped}`; |
| 60 | + document.getElementById("solvedExamples").textContent = `${solvedText} ${solved}`; |
| 61 | + document.getElementById("mistakes").textContent = `${mistakesText} ${mistakes}`; |
| 62 | +}; |
| 63 | + |
| 64 | +function nextExample() { |
| 65 | + generateExample(); |
| 66 | + document.getElementById("example").textContent = `${a}${sign}${b}=`; |
| 67 | + document.getElementById("inputField").value = ""; |
| 68 | +} |
| 69 | + |
| 70 | +function checkExample() { |
| 71 | + let correctAnswer; |
| 72 | + |
| 73 | + if (sign === "+") { |
| 74 | + correctAnswer = a + b; |
| 75 | + } else if (sign === "-") { |
| 76 | + correctAnswer = a - b; |
| 77 | + } else if (sign === "*") { |
| 78 | + correctAnswer = a * b; |
| 79 | + } else if (sign === "/") { |
| 80 | + correctAnswer = a / b; |
| 81 | + }; |
| 82 | + |
| 83 | + if (Number(document.getElementById("inputField").value) === correctAnswer) { |
| 84 | + document.getElementById("correctlySound").play(); |
| 85 | + document.getElementById("correctlyDiv").style.visibility = "visible"; |
| 86 | + setTimeout(() => { |
| 87 | + document.getElementById("correctlyDiv").style.visibility = "hidden"; |
| 88 | + }, 1000); |
| 89 | + solved++; |
| 90 | + document.getElementById("solvedExamples").textContent = `${solvedText} ${solved}`; |
| 91 | + nextExample(); |
| 92 | + } |
| 93 | + else if (document.getElementById("inputField").value === "") { |
| 94 | + } |
| 95 | + else { |
| 96 | + document.getElementById("wronglySound").play(); |
| 97 | + document.getElementById("wronglyDiv").style.visibility = "visible"; |
| 98 | + document.getElementById("correctAnswerP").textContent = `${correctAnswerPText} ${correctAnswer}`; |
| 99 | + setTimeout(() => { |
| 100 | + document.getElementById("wronglyDiv").style.visibility = "hidden"; |
| 101 | + }, 1000); |
| 102 | + mistakes++; |
| 103 | + document.getElementById("mistakes").textContent = `${mistakesText} ${mistakes}`; |
| 104 | + nextExample(); |
| 105 | + }; |
| 106 | +}; |
| 107 | + |
| 108 | +function skipExample() { |
| 109 | + skipped++; |
| 110 | + document.getElementById("skippedExamples").textContent = `${skippedText} ${skipped}`; |
| 111 | + nextExample(); |
| 112 | +}; |
| 113 | + |
| 114 | +function changeMode(newSign, newVariable, value, optionName) { |
| 115 | + sign = newSign; |
| 116 | + variable = newVariable; |
| 117 | + |
| 118 | + document.getElementById("select").value = value; |
| 119 | + |
| 120 | + generateExample(); |
| 121 | + resetCounters(); |
| 122 | + closeSidebar(); |
| 123 | + |
| 124 | + document.querySelectorAll(".sidebarOption").forEach(option => { |
| 125 | + option.disabled = false; |
| 126 | + option.classList.remove("open"); |
| 127 | + }); |
| 128 | + |
| 129 | + document.getElementById(optionName).disabled = true; |
| 130 | + document.getElementById(optionName).classList.toggle("open"); |
| 131 | +} |
| 132 | + |
| 133 | +document.addEventListener("keydown", function(event) { |
| 134 | + if (event.key === "Enter") { |
| 135 | + checkExample(); |
| 136 | + }; |
| 137 | +}); |
| 138 | + |
| 139 | +// App logic |
| 140 | + |
| 141 | +if (translations[browserLanguage]) { |
| 142 | + updateLanguage(browserLanguage); |
| 143 | +}; |
| 144 | + |
| 145 | +let skipped = 0; |
| 146 | +let solved = 0; |
| 147 | +let mistakes = 0; |
| 148 | + |
| 149 | +const skippedText = document.getElementById("skippedExamples").textContent; |
| 150 | +const solvedText = document.getElementById("solvedExamples").textContent; |
| 151 | +const mistakesText = document.getElementById("mistakes").textContent; |
| 152 | +const correctAnswerPText = document.getElementById("correctAnswerP").textContent; |
| 153 | + |
| 154 | +let a, b, sign, variable; |
| 155 | + |
| 156 | +changeMode("+", 1, "1", "add100"); |
0 commit comments