0

I am trying to find common strings in given two strings.

Example:

string1 = "mega,cloud,two,website,final"
string2 = "window,penguin,literature,network,fun,cloud,final,sausage"
answer = "cloud,final,two"

So far this is what i got:

function commonWords(first, second) {
    var words = first.match(/\w+/g);
    var result = "";
    words.sort();
    for(var i = 0; i < words.length; i++){
        if(second.includes(words[i])){
            result = result.concat(words[i]);
            result += ",";
        }
    }
    result = result.substr(0, result.length -1);
    return result;

}

But the result that i got is :

answer = cloud,final

Can you help me out please? First time asking question on StackOverFlow, so sorry for the typing.

4
  • 3
    Why two when there is no two string in second string Commented Jun 2, 2018 at 17:08
  • Where is two coming from? Did you forget to add it in the second string in the example? Commented Jun 2, 2018 at 17:08
  • If you want to find the two string from network then you can't use the w+ which define word boundaries ! Commented Jun 2, 2018 at 17:11
  • Okey i believe two comes from "network", which it shouldn't. I should use something different than 'includes'. Commented Jun 2, 2018 at 17:12

4 Answers 4

1

What I would do is first split on the two strings, then do a reduce and check if the other string includes the current string. If so add them to a new array.

const string1 = "mega,cloud,two,website,final"
const string2 = "window,penguin,literature,network,fun,cloud,final,sausage"

const array1 = string1.split(',')
const array2 = string2.split(',')

const result = array1.reduce((arr, val) => array2.includes(val) ? arr.concat(val) : arr, [])

console.log(result)
// Convert it to a string if desired
console.log(result.toString())

Sign up to request clarification or add additional context in comments.

Comments

0

Try the following:

string1 = "mega,cloud,two,website,final"
string2 = "window,penguin,literature,network,fun,cloud,final,sausage";
var arr1= string1.split(",");
var arr2 = string2.split(",");

var result = [];
arr1.forEach(function(str){
 arr2.forEach(function(str2){
    if(str2.indexOf(str) != -1)
      result.push(str);
 });
});
var answer = result.join(",");
console.log(answer);

Comments

0

The problem with your function is you also converting the string to into second as an array.

function commonWords(first, second) {
    var words = first.match(/\w+/g); // word is a array type
    var result = ""; 
    words.sort();
    for(var i = 0; i < words.length; i++){
        if(second.includes(words[i])){ //second should be string type
            result = result.concat(words[i]);
            result += ",";
        }
    }
    result = result.substr(0, result.length -1);
    return result;

}

Or you can try this

    first = "mega,cloud,two,website,final"
second = "window,penguin,literature,network,fun,cloud,final,sausage"

function commonWords(first, second) {
    var words = first.match(/\w+/g);
    var result = "";
    words.sort();
    for(var i = 0; i < words.length; i++){
        if(second.includes(words[i])){
            result = result.concat(words[i]);
            result += ",";
        }
    }
    result = result.substr(0, result.length -1);
    return result;

}

console.log(commonWords(first, second));

Comments

0

I would convert each string to an array of words with .split(','). Then filter the first array with .filter by checking if each item of the array is in the other one:

string1 = "mega,Cloud,two,website,final"
string2 = "window,penguin,literature,network,fun,cloud,final,sausage"

array1 = string1.toLowerCase().split(',')
array2 = string2.toLowerCase().split(',')
var a = array1.filter(word => -1 !== array2.indexOf(word));

console.log(a)

Before converting the string to an array I have applied .toLowerCase() so that the script can return cloud in the result if it appears in the first one as "Cloud" and in the second as "cloud"

1 Comment

might want to do some trimming since you introduced extra spaces

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.