Write a hasNoneLetters function that takes 2 strings phrase and blacklist and returns true, if phrase does not contain any letters from blacklist, otherwise returns false.
Comparison should be case-insensitive, it means x inside blacklist does not allow using X.
Examples:
hasNoneLetters('Mate Academy', 'pqrs') === true;
hasNoneLetters('ABC', 'a') === false;
my answer was:
function hasNoneLetters(phrase, blacklist) {
if(phrase.includes(blacklist)) {
return true;
} else {
return false;
}
}
but one test didn't passed saying 'It should return true if no matching letters' mine returned false;
CodePudding user response:
function hasNoneLetters(phrase, blacklist) {
const r = new RegExp(`[${blacklist}]`, 'i')
return Boolean(phrase.match(r))
}
console.log(hasNoneLetters('Mate Academy', 'pqrs'))
console.log(hasNoneLetters('ABC', 'a'))
CodePudding user response:
By checking with the includes
method, you're only looking if the string passed in arguments is contains inside the other string.
For example :
Phrase | blacklist | expected return | return value |
---|---|---|---|
Hello | hl | false | true |
while your code was working for substrings like this
Phrase | blacklist | expected return | return value |
---|---|---|---|
Hello | hel | true | true |
World | ld | true | true |
To make the program work, you need to loop through all the characters and check if those are included in the string !
Example
function hasNoneLetters(phrase, blacklist) {
const letters = blacklist.split('')
for (const letter of letters){
if (phrase.includes(letter)) return false
}
return true
}
console.log(hasNoneLetters("Hello world", "ab")) // true
console.log(hasNoneLetters("Hello world", "abcd")) // false
Note : as @albjerto mentionned, you should also check with case sensitivity.
This can be done using the toLowerCase
method on the phrase and the blacklist.
Example :
function hasNoneLetters(phrase, blacklist) {
const lowerPhrase = phrase.toLowerCase()
const lowerBlacklist = blacklist.toLowerCase()
const letters = lowerBlacklist.split('')
for (const letter of letters){
if (lowerPhrase.includes(letter)) return false
}
return true
}
console.log(hasNoneLetters("Hello world", "ab")) // true
console.log(hasNoneLetters("Hello world", "abcD")) // false
console.log(hasNoneLetters("HeLlO world", "O")) // false
CodePudding user response:
You have to split the blacklist string into individual characters and compare every character in phrase
with each upper- & lower-case character from blacklist
. Something like this:
function hasNoneLetters(phrase, blacklist) {
let forbidden = blacklist.split('');
return forbidden.every(char => {
return !phrase.includes(char.toLowerCase()) && !phrase.includes(char.toUpperCase());
});
}
console.log('should be true', hasNoneLetters('Abracadabra', 'mstvz'));
console.log('should be false', hasNoneLetters('Abracadabra', 'c'));
CodePudding user response:
You can just convert two strings params to either lowercase or uppercase and then loop over every character in the blacklist string
function hasNoneLetters(phrase, blacklist) {
blacklist.toLowerCase().split('').forEach(function(c) {
if(phrase.toLowerCase().includes(c) {
return true;
}
});
return false
}