0

Return true if none of the letters in the blacklist are present in the phrase. If at least one letter from blacklist is present in the phrase return false;

Comparison should be case insensitive. Meaning 'A' == 'a'.

/**
     * 
     * @param {string} blacklist
     * @param {string} phrase
     * 
     * @returns {boolean}
     */
    function hasNoneLetters(blacklist, phrase) {
      console.log(blacklist)
      var x = phrase.includes(blacklist);
      if(x === false){
        return true
      }
      else{
        return false
      }

    }
  • I think the blacklist needs to be split into an array if it was provided as a string. The make every thing to lowercase or uppercase to compare the values. Hope this helps. – Don F. May 30 '20 at 19:03

5 Answers5

2

It's not working because you are checking if one string is included in the other in entirety. Instead, you need to get each character of blacklist separately and check if it is present in the phrase string.

Also, use toLowerCase on the strings for case-insensitive comparison

function hasNoneLetters(blacklist, phrase) {
  blacklist = blacklist.toLowerCase();
  phrase = phrase.toLowerCase();

  for (const char of blacklist)
    if (phrase.includes(char))
      return false;

  return true
}

console.log(hasNoneLetters('abc', 'This has'))
console.log(hasNoneLetters('XYZ', 'This doesnt'))
console.log(hasNoneLetters('C', 'case insensitive'))

Another option is to create a Set of the blacklisted letters. Use Array.from to convert the string string to an array of characters. Check if some of the letters are included in the set.

function hasNoneLetters(blacklist, phrase) {
  const set = new Set(blacklist.toLowerCase());
  return !Array.from(phrase.toLowerCase()).some(c => set.has(c));
}
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 1
    Thank u very match, sorry, I am nooob, this is very difficult for me ES6 understanding – Jessica Bulldog May 30 '20 at 19:04
  • Upvoted as this is the only answer so far to include case-insensitivity. However, [Turkish case conversion in JavaScript](https://stackoverflow.com/q/1850232/1115360) has some important information to do with that. – Andrew Morton May 30 '20 at 19:08
  • @JessicaBulldog I added version similar to your code – adiga May 30 '20 at 19:10
0
/**
 * 
 * @param {string} blacklist
 * @param {string} phrase
 * 
 * @returns {boolean}
 */
function hasNoneLetters(blacklist, phrase) {
  console.log(blacklist)
  var phrase_parts = phrase.split(" ");
  for(var part of phrase_parts) {
    if (blacklist.includes(part)) {
      return false;
    }
  }
  return true;
}
Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
0

You need to check for every letter in the blacklist and not the full blacklist at once.

function hasNoneLetters(blacklist, phrase) {
  let lowerCasePhrase = phrase.toLowerCase();
  for (let letter of blacklist.toLowerCase()) {
    if (lowerCasePhrase.includes(letter)) return false
  }
  return true
}

Also, you might want to use a Set where you insert all the letters of the phrase in to optimise the performance. This will get your running time from O(N*M) down to O(max(N, M)), where N and M are the lengths of the inputs.

Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
0

You aren't asking a question - with what are you struggling?

If you're wondering why your function isn't working, var x = phrase.includes(blacklist);, will only return true if the entire blacklist appears, in order, within phrase.

0

The shortest one answer

function hasNoneLetters(blacklist, phrase) {
    return !blacklist.split('').find(b => phrase.includes(b))
 }

    function hasNoneLetters(blacklist, phrase) {
        return !blacklist.split('').find(b => phrase.includes(b))
     }

console.log(hasNoneLetters('Abc', 'the Phrase'))

console.log(hasNoneLetters('Abc', 'the PhrAse'))
console.log(hasNoneLetters('abc', 'the Phrase'))
shutsman
  • 2,357
  • 1
  • 12
  • 23