0

I try to check if a word (wordToCheck) only consists of letters from an array (letters) and also contains every letter in the array only as often (or rather not more times than they are in the array) as it actually is inside of the array.

Here are examples of what the desired function should return:

checkIfWordContainsLetters("google", ["a","o","o","g","g","l","e","x"]) === true
checkIfWordContainsLetters("google", ["a","o","g","g","l","e","x"]) === false

How can I make this code work?

function checkIfWordContainsLetters(wordToCheck, letters) {
    var lettersToString = letters.toString();
    var lettersTrimmed = lettersToString.replace(/,/gi, "?");
    var regEx = new RegExp(lettersTrimmed, "gi");
    if (wordToCheck.match(regEx)!== null) {
        return true;
    }
    else return false;
}
6
  • Why do you do replace(/,/gi, "?"): this seems to implement a rule you have not explained in the question. Commented Dec 9, 2016 at 18:51
  • @trincot Because Array.prototype.toString uses commas as a default delimiter. Commented Dec 9, 2016 at 19:00
  • Oh wow, yes, thanks @MikeMcCaughan. Quite strange way to do things ;-) Commented Dec 9, 2016 at 19:01
  • javascript returns the array like this with the toString method :"a,o,o,g,g,l" so I have to cut out all the ",". And I want to check not case sensitive (hence i). And I assumed putting the ? after every letter would make the regExp work. Sorry for not mentioning it. Thanks for your answer! Commented Dec 9, 2016 at 19:05
  • OK, I added case insensitivity to the answer as well, assuming your letters array already has all in lower case. Commented Dec 9, 2016 at 19:18

2 Answers 2

3

You could use this ES6 function:

function checkIfWordContainsLetters(wordToCheck, letters){
  return !letters.reduce((a, b) => a.replace(b,''), wordToCheck.toLowerCase()).length;
}

console.log(checkIfWordContainsLetters("google", ["a","o","o","g","g","l","e","x"]));
console.log(checkIfWordContainsLetters("google", ["a","o","g","g","l","e","x"]));

The idea is to go through each letter in the letters array, and remove one (not more!) occurrence of it in the given wordToCheck argument (well, not exactly in it, but taking a copy that lacks that one character). If after making these removals there are still characters left over, the return value is false -- true otherwise.

Of course, if you use Internet Explorer, you won't have the necessary ES6 support. This is the ES5-compatible code:

function checkIfWordContainsLetters(wordToCheck, letters){
    return !letters.reduce(function (a, b) {
        return a.replace(b, '');
    }, wordToCheck.toLowerCase()).length;
}

console.log(checkIfWordContainsLetters("google", ["a","o","o","g","g","l","e","x"]));
console.log(checkIfWordContainsLetters("google", ["a","o","g","g","l","e","x"]));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your answer! Ill check it out in my code right away
1

As long as it is not the best solution for long strings for which using some clever regex is definitely better, it works for short ones without whitespaces.

function checkIfWordContainsLetters(word, letters){
  word = word.toLowerCase().split('');

  for(var i = 0; i < letters.length; i++) {
    var index = word.indexOf( letters[i].toLowerCase() );
    if( index  !== -1 ) {
      // if word contains that letter, remove it
      word.splice( index , 1 );
      // if words length is 0, return true
      if( !word.length ) return true;
    }
  }
  return false;

}
checkIfWordContainsLetters("google", ["a","o","o","g","g","l","e","x"]); // returns true
checkIfWordContainsLetters("google", ["a","o","g","g","l","e","x"]); // returns false

4 Comments

thanks a ton! I will give this also a try right away in my code. That was also my first idea but then I got confused with all these regExps and wanted to give those a try instead. anyhow thx so much!
Hope it helps you :)
so far it works in my code but Ill have to do some mre tries as my "solution" did work for some as well. thanks again and Im of course no going to forget to mark it as answered as soon as Im done testing
Of course. Please show me examples of your test on which my code fails, if any. I also added case-insensitivity in my latest edit if there's a case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.