How can I check a word is made of letters in different array How can I check a word is made of letters in different array vue.js vue.js

How can I check a word is made of letters in different array

Your return statement is exiting the loop after the 1st iteration.

One way to do it is to use Array.every to verify all the letters and String.includes to check if the letter is part of the accepted String

const randomString = "ABEOHSTD";console.log(isWordValid(randomString, "BASE"));console.log(isWordValid(randomString, "BASEU"));function isWordValid(validLetters, attemtedWord) {  const attemptedWordSplitted = attemtedWord.split("");  return attemptedWordSplitted.every(attemptedLetter => validLetters.includes(attemptedLetter));}

If you don't allow to reuse the same letter multiple times, you need another approach by deleting the used letter from the list of acceptable letters

const randomString = "ABEOHSTD";console.log(isWordValid(randomString, "BASE"));console.log(isWordValid(randomString, "BAASE"));console.log(isWordValid(randomString, "BASEU"));function isWordValid(validLetters, attemptedWord) {  const validLettersSplitted = validLetters.split("");  const attemptedWordSplitted = attemptedWord.split("");  return attemptedWordSplitted.every(attemptedLetter => {    const letterIndex = validLettersSplitted.indexOf(attemptedLetter);    if(letterIndex > -1){      validLettersSplitted.splice(letterIndex, 1);      return true;    } else {      return false    }  });}

You are on the right way, You need to check each letter in the user word and check if they are in the random word. If one letter is in the random word, you remove it from the random word so it can't be use twice. :

let randomWord = "ABEOHSTD";let userWordThatFail = "BAASE";let userWord = "BASE";// optionnaly, uppercase both words.// we split into letters to make it easiers to processlet randomLetters = randomWord.split('');let userLetters = userWord.split('');let score = 0;//we parse each letter of the user inputuserLetters.forEach((letter) => {  // the letter exists in the random word.  let indexOfTheCurrentLetter = randomLetters.indexOf(letter);  // the letter exists, we remove it and increment the score.  if(indexOfTheCurrentLetter !== -1) {    randomLetters.splice(indexOfTheCurrentLetter, 1);    score++;  }});// the user input contains letters that are not in the random input.if(score < userLetters.length) {  console.log('fail');} else {  console.log('win : ' + score);}

A simple approach might be to iterate the list of valid characters for every character encountered in the string to test. Using string.includes would fall into this bracket. Problematically, that's O(n_validchars * n_testchars) time-complexity for each comparison. This might not be desirable for longer strings.

The JavaScript Set object can help here.

With this higher-order function (that leans heavily on the iterable nature of a string), you can generate a reusable function for a set of valid characters:

function testFor(validChars) {  const charSet = new Set(validChars);  return testString =>, c => charSet.has(c));}// And to use it:const testForABC = testFor("ABC"); //returns a functionconsole.log(testForABC("AABBCC")); //trueconsole.log(testForABC("abc")); //false

Now, because Set lookups are O(1), we're looking at O(n) complexity where n is the length of the string we're testing. Much better.