summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-04-18 19:40:15 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-04-18 19:40:15 +0800
commit11d66c51f8b5e2cd9613a87637fbc700235f1687 (patch)
treeeb7c4f6e77c61840e4af238545d17ce9e3668554
parentcb004f9d272c98b299740c32b14a77381456d34f (diff)
Add second hint: reveal words before and after
-rw-r--r--index.js38
1 files changed, 32 insertions, 6 deletions
diff --git a/index.js b/index.js
index 5391d7b..79e0dd3 100644
--- a/index.js
+++ b/index.js
@@ -12,7 +12,7 @@ const trackElem = document.querySelector("#track");
const linesElem = document.querySelector("#lines");
let currentWord = {};
-let hintTaken = false;
+let hintsLeft = 2;
let songsPicked = 0;
function newWord() {
@@ -38,7 +38,7 @@ function newWord() {
}
wordElem.innerHTML = currentWord.word;
- hintTaken = false;
+ hintsLeft = 2;
// clean after the mess
for (let elem of [inputElem, nextElem, hintElem, showAnswerElem]) {
@@ -144,7 +144,7 @@ function pickSong(event) {
showAnswer();
nextElem.focus();
} else {
- if (!hintTaken) {
+ if (hintsLeft > 0) {
reportElem.innerHTML = "Sorry, not this song :) <br /> Try a hint?";
} else {
reportElem.innerHTML = "Not this one…";
@@ -157,12 +157,38 @@ function pickSong(event) {
}
function hint() {
- if (!hintTaken) {
- answerElem.hidden = false;
- hintTaken = true;
+ if (hintsLeft == 0) { return; }
+
+ answerElem.hidden = false;
+
+ if (hintsLeft == 2) {
+ // reveal album title
albumElem.innerHTML = currentWord.album;
+ } else if (hintsLeft == 1) {
+ // reveal part of a line surrounding the word
+ // thanks for the idea, u/Tarnoo
+ const lines = currentWord.lines;
+ const randLine = lines[Math.floor(Math.random() * lines.length)];
+ const wordsInLine = randLine.split(" ");
+ // normalize line and word, then locate word
+ const normWordsInLine = randLine.toLowerCase().replace(/[,\.\"\?!]/g, "").split(" ");
+ const wordIdx = normWordsInLine.indexOf(currentWord.word.toLowerCase());
+
+ let hint = "";
+ wordsInLine.forEach((word, idx) => {
+ if (Math.abs(idx - wordIdx) <= 1) {
+ hint += word;
+ } else {
+ hint += "_".repeat(word.length);
+ }
+
+ hint += " ";
+ });
+
+ linesElem.innerHTML = hint;
hintElem.disabled = true;
}
+ hintsLeft--;
}
function showAnswer() {