Let's consider having two strings that look something like this
var tester = "hello I have to ask you a doubt";
var case = "hello better explain me the doubt";
In this scenario, both strings contain common words such as hello
and doubt
. Let's assume the default string is tester
, and we have another variable called case
which holds a set of words. The objective is to determine the count of common words present in both tester
and case
, returning the result in the form of an object.
Desired Result
{"hello" : 1, "doubt" : 1};
The current implementation approach is outlined below
var tester = "hello I have to ask you a doubt";
function getMeRepeatedWordsDetails(case){
var defaultWords = tester.split(" ");
var testWords = case.split(" "), result = {};
for(var testWord in testWords){
for(var defaultWord in defaultWords){
if(defaultWord == testWord){
result[testWord] = (!result[testWord]) ? 1 : (result[testWord] + 1);
}
}
}
return result;
}
I am considering whether Regex may offer a more efficient way to accomplish this task by identifying pattern matches. Your feedback on whether my current approach is correct or if utilizing Regex would be beneficial is appreciated.