I’m studying JavaScript, and as an exercise, I decided to copy a little game I like to help me learn, but I’m struggling and would like to know if someone can explain how to solve this.
I don’t like asking for help, but after two days of struggling and asking AI, I decided to come here.
The game consists of guessing the word of the day, and it has the following behavior:
- If the letter exists in the word of the day, mark it as
isPresent
true; - If the letter exists in the exact same position as in the word of the day, mark it as
isCorrect
true; - If the first letter matches the first letter in the word of the day, mark it as
isFirst
true; - If the last letter matches the last letter in the word of the day, mark it as
isLast
true.
I’m having a lot of difficulty with this part:
- If there are letters in the same sequence as in the word of the day, mark them as
isNeighbor
. For example, if the word of the day is “example” and the guess is “imply”, “mpl” should getisNeighbor
true. I wrote this part, but it doesn’t work in all cases; - If the letters are not in the exact same position in the guess and in the word of the day, but they are in the same order, they should be marked as
isCorrect
true. If the word of the day is “example” and the guess is “permit”, “p” and “e” would beisCorrect
, but “m” wouldn’t, as in “example”, it appears before “p”. I simply don’t know what to do, and I’ve been stuck here for two days.
Here’s a screenshot of the game instructions I’m copying, so you can understand better:
My code looks like this:
// Word of the day
const WORD_OF_THE_DAY = "example";
// Main function
const guessWord = (inputValue) => {
let originalLetterUsed = Array(WORD_OF_THE_DAY.length).fill(false);
/**
* Finds the index of the first occurrence of an unused letter from the word of the day.
* @param {string} letter - Letter to find in the word of the day.
* @returns {number} - The index of the letter, or -1 if not found.
*/
const findAvailableLetterIndex = (letter) => WORD_OF_THE_DAY.split('').findIndex((char, charIdx) => {
return char === letter && !originalLetterUsed[charIdx];
});
const word = [...inputValue].map((letter, letterIdx) => {
const availableIndex = findAvailableLetterIndex(letter);
let currentLetter = {
letter,
isCorrect: false,
isPresent: false,
isNeighbor: false,
isFirst: false,
isLast: false
}
// If the letter exists in the word of the day
if (availableIndex > -1) {
originalLetterUsed[availableIndex] = true;
currentLetter.isPresent = true;
// Checks if the next letter in the typed word matches the next letter of the word of the day,
// or if the previous letter in the typed word matches the previous letter of the word of the day
if (
inputValue[letterIdx + 1] === WORD_OF_THE_DAY[availableIndex + 1] ||
inputValue[letterIdx - 1] === WORD_OF_THE_DAY[availableIndex - 1]
) {
currentLetter.isNeighbor = true;
}
// Check if the current letter mathes with the letter of the word of the day in the same position
if (letter === WORD_OF_THE_DAY[availableIndex]) {
currentLetter.isCorrect = true;
}
// Check if the letter is in the correct order
/* if (???) {
currentLetter.isCorrect = true;
} */
// Check if the first letter matches
if (letterIdx === 0 && letter === WORD_OF_THE_DAY[0]) {
currentLetter.isFirst = true;
}
// Check if the last letter matches
if (letterIdx === inputValue.length - 1 && letter === WORD_OF_THE_DAY[WORD_OF_THE_DAY.length - 1]) {
currentLetter.isLast = true;
}
}
return currentLetter;
});
return word;
}
console.log("permit", guessWord("permit"));
console.log("imply", guessWord("imply"));
console.log("excuse", guessWord("excuse"));
console.log("example", guessWord("example"));