Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript to check if string has at least three characters that are in another variable

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!

like image 880
user10930711 Avatar asked Aug 07 '20 15:08

user10930711


People also ask

How do you check if a string contains a specific character in JavaScript?

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.

How do I find the first 3 characters of a string?

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.

How do you check if a string contains a value in JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false .

How do you check if a string has any characters?

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 .


3 Answers

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);
like image 162
gaetanoM Avatar answered Oct 27 '22 19:10

gaetanoM


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`);
like image 29
Premal Katigar Avatar answered Oct 27 '22 18:10

Premal Katigar


Use For loop with break and Use includes()

To be clean, make a function that will do the job.

To be efficent use break when 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'));
like image 24
Omer Avatar answered Oct 27 '22 20:10

Omer