diff options
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 38 |
1 files changed, 32 insertions, 6 deletions
@@ -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() { |