Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
resolves #6
Browse files Browse the repository at this point in the history
  • Loading branch information
aceakash committed May 22, 2017
1 parent 2006d52 commit 013ac3a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
47 changes: 47 additions & 0 deletions compare-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ exports.compareTwoStrings = compareTwoStrings;
exports.findBestMatch = findBestMatch;

function compareTwoStrings(str1, str2) {
var result = null;
result = calculateResultIfIdentical(str1, str2);
if (result != null) {
return result;
}
result = calculateResultIfEitherIsEmpty(str1, str2);
if (result != null) {
return result;
}
result = calculateResultIfBothAreSingleCharacter(str1, str2);
if (result != null) {
return result;
}

var pairs1 = wordLetterPairs(str1.toUpperCase());
var pairs2 = wordLetterPairs(str2.toUpperCase());
var intersection = 0;
Expand Down Expand Up @@ -39,6 +53,39 @@ function compareTwoStrings(str1, str2) {
function wordLetterPairs(str) {
return _flattenDeep(_map(str.split(' '), letterPairs));
}

function isEdgeCaseWithOneOrZeroChars(str1, str2) {
if (str1.length == str2.length && str1.length == 1) {
return true;
}
return false;
}

function calculateResultIfIdentical(str1, str2) {
if (str1.toUpperCase() == str2.toUpperCase()) {
return 1;
}
return null;
}

function calculateResultIfBothAreSingleCharacter(str1, str2) {
if (str1.length == 1 && str2.length == 1) {
return 0;
}
}

function calculateResultIfEitherIsEmpty(str1, str2) {
// if both are empty strings
if (str1.length == 0 && str2.length == 0) {
return 1;
}

// if only one is empty string
if ((str1.length + str2.length) > 0 && (str1.length * str2.length) == 0) {
return 0;
}
return null;
}
}


Expand Down
45 changes: 19 additions & 26 deletions compare-strings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,25 @@ describe('compareTwoStrings', function () {
expect(typeof compareTwoStrings).toBe('function');
});

it('returns 0 for totally different strings: french and quebec', function () {
expect(compareTwoStrings('french', 'quebec')).toEqual(0);
});

it('returns 1 for identical strings: france and france', function () {
expect(compareTwoStrings('france', 'france')).toEqual(1);
});

it('returns 1 for same but differently cased strings: fRaNce and france', function () {
expect(compareTwoStrings('fRaNce', 'france')).toEqual(1);
});

it('returns 0.8 for healed and sealed', function () {
expect(compareTwoStrings('healed', 'sealed')).toEqual(0.8);
});

it('returns 0.896551724137931 for "web applications" and "applications of the web"', function () {
expect(compareTwoStrings('web applications', 'applications of the web')).toEqual(0.896551724137931);
});

it('returns 0.9 for "this will have a typo somewhere" and "this will huve a typo somewhere"', function () {
expect(compareTwoStrings("this will have a typo somewhere", "this will huve a typo somewhere")).toEqual(0.9);
});

it('returns 0.8333333333333334 for "this has one extra word" and "this has one word"', function () {
expect(compareTwoStrings("this has one extra word", "this has one word")).toEqual(0.8333333333333334);
it('returns the correct value for different inputs:', function () {
const testData = [
{first: 'french', second: 'quebec', expected: 0},
{first: 'france', second: 'france', expected: 1},
{first: 'fRaNce', second: 'france', expected: 1},
{first: 'healed', second: 'sealed', expected: 0.8},
{first: 'web applications', second: 'applications of the web', expected: 0.896551724137931},
{first: 'this will have a typo somewhere', second: 'this will huve a typo somewhere', expected: 0.9},
{first: 'this has one extra word', second: 'this has one word', expected: 0.8333333333333334},
{first: 'a', second: 'a', expected: 1},
{first: 'a', second: 'b', expected: 0},
{first: '', second: '', expected: 1},
{first: 'a', second: '', expected: 0},
{first: '', second: 'a', expected: 0}
];

testData.forEach(td => {
expect(compareTwoStrings(td.first, td.second)).toEqual(td.expected);
});
});
});

Expand Down

0 comments on commit 013ac3a

Please sign in to comment.