kanaguessr/index.js

154 lines
4.9 KiB
JavaScript
Raw Normal View History

2021-01-22 00:25:31 +01:00
$(function() {
document.querySelector("#answer_field").addEventListener("keyup", event => {
2021-03-06 00:15:49 +01:00
if (event.key !== "Enter") {return} // The only key we care about is Enter
2021-01-22 00:25:31 +01:00
document.getElementById("correct_answer").style.visibility == "visible" ? document.querySelector("#next_btn").click() : document.querySelector("#answer_btn").click()
})
})
function changeQuestion() {
2021-03-06 00:15:49 +01:00
document.getElementById("answer_field").value = "" // Empty field so user doesn't have to do it
document.getElementById("answer_field").focus() // Focus on field so user doesn't have to do it
2021-01-22 00:25:31 +01:00
let questionShape = shapeQuestion([document.getElementById('h').checked, document.getElementById('k').checked, document.getElementById('r').checked])
if (!questionShape) { // No question can be found if two boxes are left unchecked
document.getElementById("allowed").style.fontSize = "30px"
$("#allowed").animate({"fontSize": "20px"}, 800)
return
}
2021-01-22 00:25:31 +01:00
2021-03-06 00:15:49 +01:00
changeBackground(questionShape[1], document.getElementById("character"))
changeBackground(questionShape[0], document.getElementById("equivalent"))
2021-01-27 02:37:57 +01:00
2021-03-06 00:15:49 +01:00
document.getElementById("correct_answer").style.visibility = "hidden"
$("#correct_answer").slideUp(10)
2021-01-22 00:25:31 +01:00
$.getJSON("characters.json", function(data) {
const characters = data.items[Math.floor(Math.random() * data.items.length)]
2021-03-06 00:15:49 +01:00
var char_in_box
var equivalent_of_char
2021-01-22 00:25:31 +01:00
var answer
2021-03-06 00:15:49 +01:00
switch (questionShape[0]) {
2021-01-22 00:25:31 +01:00
case "h":
2021-03-06 00:15:49 +01:00
equivalent_of_char = "hiragana"
answer = characters.h
2021-01-22 00:25:31 +01:00
break
case "k":
2021-03-06 00:15:49 +01:00
equivalent_of_char = "katakana"
answer = characters.k
2021-01-22 00:25:31 +01:00
break
case "r":
2021-03-06 00:15:49 +01:00
equivalent_of_char = "romaji"
answer = characters.r
2021-01-22 00:25:31 +01:00
break
2021-03-06 00:15:49 +01:00
default: // This part of the code can't get executed in theory
equivalent_of_char = "???"
2021-01-22 00:25:31 +01:00
}
2021-03-06 00:15:49 +01:00
switch (questionShape[1]) {
2021-01-22 00:25:31 +01:00
case "h":
2021-03-06 00:15:49 +01:00
char_in_box = characters.h
2021-01-22 00:25:31 +01:00
break
case "k":
2021-03-06 00:15:49 +01:00
char_in_box = characters.k
2021-01-22 00:25:31 +01:00
break
case "r":
2021-03-06 00:15:49 +01:00
char_in_box = characters.r
2021-01-22 00:25:31 +01:00
break
2021-03-06 00:15:49 +01:00
default: // This part of the code can't get executed in theory
char_in_box = "???"
2021-01-22 00:25:31 +01:00
}
2021-03-06 00:15:49 +01:00
document.getElementById("equivalent").innerHTML = equivalent_of_char
document.getElementById("character").innerHTML = char_in_box
document.getElementById("correct_answer").getElementsByTagName('p')[0].innerHTML = `the answer was ${answer}`
2021-01-22 00:25:31 +01:00
})
}
2021-01-27 02:37:57 +01:00
function changeBackground(char, para) {
switch (char) {
case "h":
para.style.backgroundColor = "red"
break;
case "k":
para.style.backgroundColor = "green"
break;
case "r":
para.style.backgroundColor = "blue"
break;
default:
para.style.backgroundColor = "black"
}
}
2021-01-22 00:25:31 +01:00
function shapeQuestion(allow) {
if (allow.filter(Boolean).length < 2) {return false} // No question can be found if two boxes are left unchecked
2021-01-22 00:25:31 +01:00
let possibilities = ['h', 'k', 'r']
var element_one
var element_two
while (element_one == undefined) {
let x = Math.floor(Math.random() * possibilities.length)
if (allow[x]) {
let temp = possibilities[x]
allow.splice(possibilities.indexOf(temp), 1)
possibilities.splice(possibilities.indexOf(temp), 1)
element_one = temp
}
}
while (element_two == undefined) {
let x = Math.floor(Math.random() * possibilities.length)
if (allow[x] && possibilities[x] != element_one) {element_two = possibilities[x]}
}
return [element_one, element_two]
}
function checkCharacters() {
let answer_field = document.getElementById("answer_field")
let equivalent = document.getElementById("equivalent")
const REGEX_JAPANESE = /[\u3040-\u30ff]/
const REGEX_OTHER = /[^\u3040-\u30ff]/
if (!answer_field.value) {
answer_field.style.backgroundColor = "black"
return
}
if (equivalent.innerHTML == "romaji") {
answer_field.value.match(REGEX_JAPANESE) ? answer_field.style.backgroundColor = "red" : answer_field.style.backgroundColor = "black"
} else {
answer_field.value.match(REGEX_OTHER) ? answer_field.style.backgroundColor = "red" : answer_field.style.backgroundColor = "black"
}
}
function checkAnswer() {
2021-01-22 00:25:31 +01:00
let answer_field = document.getElementById("answer_field")
let correct_answer_p = document.getElementById("correct_answer").getElementsByTagName('p')[0]
2021-01-27 16:13:40 +01:00
let positive = document.getElementById("positive")
let negative = document.getElementById("negative")
2021-01-22 00:25:31 +01:00
if (answer_field.style.backgroundColor == "red") {
document.getElementById("equivalent").style.fontSize = "23px"
$("#equivalent").animate({"fontSize": "20px"}, 500)
return
}
2021-01-27 02:37:57 +01:00
if (answer_field.value.toLowerCase() == correct_answer_p.innerHTML.slice(correct_answer_p.innerHTML.lastIndexOf(" ") + 1)) {
positive.style.fontSize = "25px"
$("#positive").animate({"fontSize": "20px"}, 250)
2021-01-22 00:25:31 +01:00
positive.innerHTML = Number(positive.innerHTML) + 1
} else {
negative.style.fontSize = "25px"
$("#negative").animate({"fontSize": "20px"}, 250)
2021-01-22 00:25:31 +01:00
negative.innerHTML = Number(negative.innerHTML) + 1
}
correct_answer.style.visibility = "visible"
$("#correct_answer").slideDown(100)
2021-01-27 16:13:40 +01:00
if (document.getElementById('auto').checked) {changeQuestion()}
2021-01-22 00:25:31 +01:00
}