0

Have tried a variety of things but nothing is quite working...

I have the following code:

        correctList(id, e) {
          if (this.regex.test(e.key)) {
            this.correctLetters.push([e.key, id]);
          }

          this.correctLetters.forEach(
            (array) => {
              if (this.filteredWords.length > 1) {
                this.filteredList = this.filteredWords.filter((word) => word.indexOf(array[0]) === array[1]);
            } else {
                this.filteredList = this.FiveLetterWords.filter((word) => word.indexOf(array[0]) === array[1]);
            }
            this.setFilteredWords(this.filteredList);
            }
          );
        },

Where 'this.correctLetters[]' is:

[[e,0], [l,1], [d,2]...]

This is built up using inputs and the order they are typed is the index number ( the 0, 1, 2 in the arrays)

and

'this.filteredWords' is:

["cigar", "rebut", "sissy", "humph", "awake", "blush",  "focal", "evade", "naval", "serve", "heath", "dwarf", "model", "karma", "stink", "grade", "quiet", "bench", "abate", "feign", "major", "death", "fresh", "crust", "stool", "colon", "abase", "marry", "react", "batty", "pride", "floss", "helix", "croak", "staff", "paper", "unfed", "whelp", "trawl", "outdo", "adobe", "crazy", "sower", "repay", "digit", "crate", "cluck", "spike", "mimic", "elder".....]

There are many more, but not really relevant to list all for question

My problem is the filter... Take the Word "elder"... If I type "e", "l" , "d", then I will get the list filters by words starting with "e", then filtered by words starting "el" and then filtered but words staring "eld".But as soon as I type "elde", it filters the array to empty (which I understand is technically correct due to my filtering method currently) and then if I type "elder", I will get all words ending in "r".

So essentially I understand my problem is the indexOf(() part of things as it`s only matching the first instance of the letter in the word, but I want to filter on words where the letter === the index of that letter but I am not sure how to change things. So if I type E R then right ow, I get only ending in R words, I want any words ending ER, including any that START with E also, or have another E in there...

Hopefully this makes some sense, am going round in circles with this.... Somebody suggested regex, but my first few attempts didn`t help, but possibly doing it wrong...

EDIT: Sort of fixed it with:

correctList(id, e) {
    if (this.regex.test(e.key)) {
    this.correctLetters.push([e.key, id]);
    }

    this.correctLetters.forEach(
    (array) => {
        if (this.filteredWords.length > 1) {
        this.filteredList = this.filteredWords.filter(
            (word) => {
                const wordSplit = word.split('');
                return wordSplit[array[1]] === array[0];
            }
        );
    } else {
        this.filteredList = this.FiveLetterWords.filter(
            (word) => {
                const wordSplit = word.split('');
                return wordSplit[array[1]] === array[0];
            }
        );
    }
    console.log(this.filteredList);
    this.setFilteredWords(this.filteredList);
    }
    );

But not sure if there is a more elegant method...

  • 1
    I don't know if this is a problem for you. But I just was warned about using regex.test - it doesn't automatically move lastIndex back to -1, so your next call to test may fail unexpectedly. – ControlAltDel Feb 08 '22 at 16:24
  • Its not immediately clear what you're trying to do - Can you include a [mcve] in your question demonstrating some inputs and expected outputs? – Jamiec Feb 08 '22 at 16:27
  • Why not use `word[array[1]] == array[0]` instead of using `indexOf()`? – Barmar Feb 08 '22 at 16:29
  • @Jamiec It's Wordle, don't you think? Trying to make sure that the letters of the guess match the position in which they appear in the candidate word. – Wyck Feb 08 '22 at 16:30
  • @Wyck sounds plausible. Would like confirmation from OP before I write an answer – Jamiec Feb 08 '22 at 16:31
  • ControlAltDel is referring to [Why does a RegExp with global flag give wrong results?](https://stackoverflow.com/questions/1520800/why-does-a-regexp-with-global-flag-give-wrong-results) which can happen if you specify the `g` flag on your RegExp. – Wyck Feb 08 '22 at 16:31
  • @Wyck - Yeah, I was playing with a Wordle Solver thing (mainly so I could play with Vue a bit)... The Letters being entered would match the order they appear in the Wordle game as green, so the letters can be entered in any order... – Paul Jeffreys Feb 08 '22 at 18:11
  • @Jamiec - Yup, see above (can only add 1 @ in a reply apparently. I have the wrong letters and right letters, wrong location working fine., it`s just the correct (green) letters when there are more than 1 in the word. – Paul Jeffreys Feb 08 '22 at 18:12

2 Answers2

0

I may have completely missed the point here, but it looks like you're just trying to filter a list of words that contain what the user has typed/selected somewhere.... in which case its as easy as

const words = filteredWords.filter(x => x.indexOf(something) > -1);

An example of this can be seen below, (start typing er and you'll get any word with "er" in it somewhere):

const filteredWords = ["cigar", "rebut", "sissy", "humph", "awake", "blush",  "focal", "evade", "naval", "serve", "heath", "dwarf", "model", "karma", "stink", "grade", "quiet", "bench", "abate", "feign", "major", "death", "fresh", "crust", "stool", "colon", "abase", "marry", "react", "batty", "pride", "floss", "helix", "croak", "staff", "paper", "unfed", "whelp", "trawl", "outdo", "adobe", "crazy", "sower", "repay", "digit", "crate", "cluck", "spike", "mimic", "elder"]

document.getElementById("entry").addEventListener("input", e => {
  const words = filteredWords.filter(x => x.indexOf(e.target.value)>-1);
  document.getElementById("output").value = words.join("\r\n")
})
<input id="entry" type="text">
<br>
<textarea id="output" rows="10"></textarea>
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

To get the set of words that have the given letters in the given positions, just do this...

filteredList = FiveLetterWords.filter(
    word => correctLetters.reduce(
        (prev, [letter, index]) => prev && (word[index] === letter),
        true
    )
);
Kevin Perry
  • 541
  • 3
  • 6