Home:ALL Converter>Return true if none of the letters in the blacklist

Return true if none of the letters in the blacklist

Ask Time:2020-05-31T02:50:14         Author:Jessica Bulldog

Json Formatter

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
      }

    }

Author:Jessica Bulldog,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62107239/return-true-if-none-of-the-letters-in-the-blacklist
adiga :

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. \n\nAlso, use toLowerCase on the strings for case-insensitive comparison\n\n\r\n\r\nfunction hasNoneLetters(blacklist, phrase) {\r\n blacklist = blacklist.toLowerCase();\r\n phrase = phrase.toLowerCase();\r\n\r\n for (const char of blacklist)\r\n if (phrase.includes(char))\r\n return false;\r\n\r\n return true\r\n}\r\n\r\nconsole.log(hasNoneLetters('abc', 'This has'))\r\nconsole.log(hasNoneLetters('XYZ', 'This doesnt'))\r\nconsole.log(hasNoneLetters('C', 'case insensitive'))",
2020-05-30T18:58:20
Simon Lutterbie :

You aren't asking a question - with what are you struggling?\n\nIf 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.",
2020-05-30T18:57:16
Aziz Sonawalla :

/**\n * \n * @param {string} blacklist\n * @param {string} phrase\n * \n * @returns {boolean}\n */\nfunction hasNoneLetters(blacklist, phrase) {\n console.log(blacklist)\n var phrase_parts = phrase.split(\" \");\n for(var part of phrase_parts) {\n if (blacklist.includes(part)) {\n return false;\n }\n }\n return true;\n}\n",
2020-05-30T18:56:33
Ahmed Hammad :

You need to check for every letter in the blacklist and not the full blacklist at once.\n\nfunction hasNoneLetters(blacklist, phrase) {\n let lowerCasePhrase = phrase.toLowerCase();\n for (let letter of blacklist.toLowerCase()) {\n if (lowerCasePhrase.includes(letter)) return false\n }\n return true\n}\n\n\nAlso, 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.",
2020-05-30T18:56:44
yy