JSFiddle - React, Tailwind, and code Playground
JavaScript
function getCommon(array1,array2){
var result = new Array();
for (i=0; i<array1.length; i++) {
for (j=0; j<array2.length; j++) {
if (array1[i] == array2[j] && jQuery.inArray(array1[i],result) == -1)
result.push(array1[i]);
}
}
return result;
}
var str1 = "apple banana cherry damson";
var str2 = "apple elderberry cherry fig";
var array1 = str1.split(/\b/);
var array2 = str2.split(/\b/);
var commonWords = getCommon(array1,array2);
alert(commonWords);