kanaguessr/index.js

158 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-01-22 00:25:31 +01:00
$(function() {
2023-03-04 16:51:27 +01:00
$("#answer_field").on("keyup", event => {
2021-03-06 00:15:49 +01:00
if (event.key !== "Enter") {return} // The only key we care about is Enter
2023-03-04 16:51:27 +01:00
$("#correct_answer").css("visibility") === "visible" ? $("#next_btn").trigger("click") : $("#answer_btn").trigger("click")
2021-01-22 00:25:31 +01:00
})
2023-03-04 16:02:54 +01:00
changeQuestion()
2023-03-06 13:32:56 +01:00
$("#answer_field").on("input propertychange", () => {checkCharacters()})
2023-03-04 16:51:27 +01:00
$("#answer_field").on("focus", () => {$("#answer_field").select()})
2023-03-04 16:02:54 +01:00
$("#answer_btn").click(checkAnswer)
$("#next_btn").click(changeQuestion)
2021-01-22 00:25:31 +01:00
})
function changeQuestion() {
2023-03-04 16:51:27 +01:00
$("#answer_field").val("") // Empty field so user doesn't have to do it
$("#answer_field").focus() // Focus on field so user doesn't have to do it
2023-03-07 23:59:19 +01:00
$("#answer_btn").attr("disabled", false)
$("#answer_btn").css("display", "block")
$("#next_btn").css("display", "none")
$("#next_btn").html("skip")
2021-01-22 00:25:31 +01:00
2023-03-06 01:07:09 +01:00
let questionShape = shapeQuestion(
[$("#hq:checked").length, $("#kq:checked").length, $("#rq:checked").length],
[$("#he:checked").length, $("#ke:checked").length, $("#re:checked").length]
)
2023-03-06 13:32:56 +01:00
if (!questionShape) { // No question can be found if settings are messed up
$("#settings").css("opacity", "0")
$("#settings").animate({"opacity": "1"}, 200)
return
}
2021-01-22 00:25:31 +01:00
2023-03-06 01:07:09 +01:00
changeBackground(questionShape[0], $("#character"))
changeBackground(questionShape[1], $("#equivalent"))
2021-01-27 02:37:57 +01:00
2023-03-04 16:51:27 +01:00
$("#correct_answer").css("visibility", "hidden")
2023-03-04 16:02:54 +01:00
$("#correct_answer").slideUp(100)
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":
2023-03-06 01:07:09 +01:00
char_in_box = characters.h
2021-01-22 00:25:31 +01:00
break
case "k":
2023-03-06 01:07:09 +01:00
char_in_box = characters.k
2021-01-22 00:25:31 +01:00
break
case "r":
2023-03-06 01:07:09 +01:00
char_in_box = characters.r
2021-01-22 00:25:31 +01:00
break
2023-03-06 13:32:56 +01:00
default:
2023-03-06 01:07:09 +01:00
char_in_box = "???"
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":
2023-03-06 13:32:56 +01:00
equivalent_of_char = "ひらがな"
2023-03-06 01:07:09 +01:00
answer = characters.h
2021-01-22 00:25:31 +01:00
break
case "k":
2023-03-06 13:32:56 +01:00
equivalent_of_char = "カタカナ"
2023-03-06 01:07:09 +01:00
answer = characters.k
2021-01-22 00:25:31 +01:00
break
case "r":
2023-03-06 13:32:56 +01:00
equivalent_of_char = "rōmaji"
2023-03-06 01:07:09 +01:00
answer = characters.r
2021-01-22 00:25:31 +01:00
break
2023-03-06 13:32:56 +01:00
default:
2023-03-06 01:07:09 +01:00
equivalent_of_char = "???"
2021-01-22 00:25:31 +01:00
}
2023-03-04 16:51:27 +01:00
$("#character").html(char_in_box)
2023-03-06 01:07:09 +01:00
$("#equivalent").html(equivalent_of_char)
2023-03-04 16:51:27 +01:00
$("#answer_paragraph").html(`the answer was ${answer}`)
2021-01-22 00:25:31 +01:00
})
}
2021-01-27 02:37:57 +01:00
function changeBackground(char, para) {
2023-03-04 16:51:27 +01:00
let colour = "black"
if (char === "h") {colour = "#b57642"}
else if (char === "k") {colour = "steelblue"}
else if (char === "r") {colour = "blueviolet"}
2023-03-04 16:51:27 +01:00
para.css("backgroundColor", colour)
2021-01-27 02:37:57 +01:00
}
2023-03-06 01:07:09 +01:00
function shapeQuestion(question, equivalent) {
2023-03-04 16:51:27 +01:00
let possibilities = ["h", "k", "r"]
2021-01-22 00:25:31 +01:00
var element_one
var element_two
2023-03-06 01:07:09 +01:00
question = question.map((a, i) => {if (a) return possibilities[i]}).filter((a) => a)
equivalent = equivalent.map((a, i) => {if (a) return possibilities[i]}).filter((a) => a)
if (question.length < 1 || equivalent.length < 1) {return false}
if (question.length === 1 && equivalent.length === 1) {
if (JSON.stringify(question) === JSON.stringify(equivalent)) {return false}
2021-01-22 00:25:31 +01:00
}
2023-03-06 01:07:09 +01:00
while (element_one === undefined || element_two === undefined) {
let x = Math.floor(Math.random() * question.length)
if (question[x]) {
element_one = question[x]
}
let y = Math.floor(Math.random() * equivalent.length)
if (equivalent[y] && equivalent[y] != element_one) {
element_two = equivalent[y]
}
2021-01-22 00:25:31 +01:00
}
return [element_one, element_two]
}
function checkCharacters() {
const REGEX_JAPANESE = /[\u3040-\u30ff]/
const REGEX_OTHER = /[^\u3040-\u30ff]/
2023-03-06 13:32:56 +01:00
let regex = $("#equivalent").html() === "rōmaji" ? REGEX_JAPANESE : REGEX_OTHER
2023-03-04 16:51:27 +01:00
if (!$("#answer_field").val() || !$("#answer_field").val().match(regex)) {
$("#answer_field").css("backgroundColor", "black")
} else {
$("#answer_field").css("backgroundColor", "red")
}
}
function checkAnswer() {
2023-03-07 23:59:19 +01:00
$("#answer_field").focus() // Keey keyboard on for mobile users
2023-03-06 20:06:43 +01:00
if (!$("#answer_field").val()) {return}
2023-03-06 13:32:56 +01:00
if ($("#answer_field").css("backgroundColor") === "rgb(255, 0, 0)") {
2023-03-06 20:06:43 +01:00
$("#equivalent").css("fontSize", "25px")
$("#equivalent").animate({"fontSize": "20px"}, 1000)
return
}
2023-03-04 16:51:27 +01:00
let html = $("#answer_paragraph").html()
let element = $("#answer_field").val().toLowerCase() === html.slice(html.lastIndexOf(" ") + 1) ? $("#positive") : $("#negative")
2023-03-04 22:49:24 +01:00
element.css("opacity", "0")
2023-03-06 20:06:43 +01:00
element.css("backgroundColor", element.css("color"))
setTimeout(() => {element.css("backgroundColor", "black")}, 125)
element.animate({"opacity": "1"}, 250)
2023-03-04 16:02:54 +01:00
element.html(Number(element.html()) + 1)
2021-01-22 00:25:31 +01:00
2023-03-06 20:06:43 +01:00
if ($("#auto:checked").length) {
changeQuestion()
} else {
$("#correct_answer").css("visibility", "visible")
$("#correct_answer").slideDown(100)
2023-03-07 23:59:19 +01:00
$("#answer_btn").attr("disabled", true)
$("#answer_btn").css("display", "none")
$("#next_btn").html("next")
$("#next_btn").css("display", "block")
2023-03-06 20:06:43 +01:00
}
2021-01-22 00:25:31 +01:00
}