I have a variable activeUserName and a variable manager1.
How can I check if activeUserName contains at least three characters, that are in manager1? (The position of those characters doesn't matter)
For example in the following case, it should return true, because the characters 'J', 'o' and 'e' are inside manager1.
var activeUserName = "JohnDoe100";
var manager1 = "JYZALoe999";
Right now I'm using the indexOf method and only look at characters in certain positions which is why I want to change that:
if (isEditor == false){
if (((activeUserName.indexOf(manager1.charAt(0)) !== -1) && (activeUserName.indexOf(manager1.charAt(2)) !== -1)) || (activeUserName.indexOf(manager1.charAt(4)) !== -1)){
// doSth();
} else if (((activeUserName.indexOf(manager2.charAt(0)) !== -1) && (activeUserName.indexOf(manager2.charAt(2)) !== -1)) || (activeUserName.indexOf(manager2.charAt(4)) !== -1)){
// doSth();
} else {
// doSth();
}
}
I read about Regex, but I'm not sure if this can be applied here.
Any help is appreciated!
Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.
Use the string. slice() method to get the first three characters of a string, e.g. const first3 = str. slice(0, 3); . The slice method will return a new string containing the first three characters of the original string.
The includes() method returns true if a string contains a specified string. Otherwise it returns false .
Use String contains() Method to Check if a String Contains Character. Java String's contains() method checks for a particular sequence of characters present within a string. This method returns true if the specified character sequence is present within the string, otherwise, it returns false .
Combining .split() with .filter() you can trasform activeUserName in array and filter each char against the string manager1:
var activeUserName = "JohnDoe100";
var manager1 = "JYZALoe999";
var howMany = activeUserName.split('').filter(function(e, i, a) {
return (manager1.indexOf(e) != -1);
}).length;
console.log('The number of chars in common is: ' + howMany);
var activeUserName = "JohnDoe100";
var manager1 = "JYZALoe999";
const arr1 = [...activeUserName];
const arr2 = [...manager1];
let count = 0;
for (let i = 0; i<arr1.length; i++) {
if (arr2.indexOf(arr1[i]) !== -1) count++;
}
if(count >= 3)
console.log(`string b 3 same values of string b`);
To be clean, make a function that will do the job.
To be efficent use
breakwhen you find 3 match chars.
Prefer not to use var, use const and let.
const areStringsMatchCharsMoreThen2 = (stringA, stringB) => {
let count = 0;
for (let i = 0; i < stringB.length; i++) {
if (count === 3) break;
if (stringA.includes(stringB.charAt(i))) count++;
}
return count === 3;
}
const activeUser = "JohnDoe100";
const manager = "JYZALoe999";
const areStringsMatchCharsMoreThen2 = (stringA, stringB) => {
let count = 0;
for (let i = 0; i < stringB.length; i++) {
if (count === 3) break;
if (stringA.includes(stringB.charAt(i))) count++;
}
return count === 3;
}
console.log(areStringsMatchCharsMoreThen2(activeUser, manager));
console.log(areStringsMatchCharsMoreThen2('ABC', 'def'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With