0

How do you detect the difference between two similar words?

For example:

  • word compared to word, gives , as a variable
  • this compared to .this gives . as a variable
  • info compared to :info, gives : and , as a variable

In this case we always know which word is the longer one. And the actual word is always the same for the comparison. It's just that the longer one maybe has some extra characters.

I need to do this using javascript only.

asked Apr 30, 2016 at 8:35
7
  • please add some more examples and their results. Commented Apr 30, 2016 at 8:36
  • 2
    longer.replace(shorter, '') Commented Apr 30, 2016 at 8:38
  • 2
    What you are looking for is called Levenshtein Distance. Commented Apr 30, 2016 at 8:38
  • @PraveenKumar that sounds a bit intense - it's not possible with javascript? Commented Apr 30, 2016 at 8:41
  • 1
    @AmyNeville See my answer. :) It's possible. The link I sent you is JavaScript. What do you think it is then? Commented Apr 30, 2016 at 8:42

2 Answers 2

3

You could try checking for difference between the array of characters.

var shortStr = 'info';
var longStr = ':info,';
console.log(Array.from(longStr).filter(function(c) {
 return Array.from(shortStr).indexOf(c) === -1;
}));
Praveen Kumar Purushothaman
167k27 gold badges215 silver badges262 bronze badges
answered Apr 30, 2016 at 8:49
Sign up to request clarification or add additional context in comments.

1 Comment

Yea, as I said you, the first code you posted doesn't work. Arrow functions do not work consistently in all the browsers.
1

Also, there's a better way, check if the string is a sub-string, and then remove the sub-string from the main string:

function checkDiff(a, b) {
 var big = '', small = '';
 if (a.length > b.length) {
 big = a;
 small = b;
 } else {
 small = a;
 big = b;
 }
 if (big.indexOf(small) != -1) {
 return big.replace(small, "");
 } else {
 return false;
 }
}
alert(checkDiff("word", "word,"));
alert(checkDiff("this", ".this"));
alert(checkDiff("info", ":info,"));

I have added demo for all your cases. It returns the values as a single string, based on the place it has occurred. You can send the output as an array as well, using the .split() method.

function checkDiff(a, b) {
 var big = '', small = '';
 if (a.length > b.length) {
 big = a;
 small = b;
 } else {
 small = a;
 big = b;
 }
 if (big.indexOf(small) != -1) {
 console.log(big.replace(small, "").split(""));
 return big.replace(small, "").split("");
 } else {
 return false;
 }
}
alert(checkDiff("word", "word,"));
alert(checkDiff("this", ".this"));
alert(checkDiff("info", ":info,"));

answered Apr 30, 2016 at 8:42

3 Comments

is it possible to return the start and end variables separately? :0
@AmyNeville Added console.log as well to check in console if it works fine. And it does return the different values as an array, on the basis of the occurrence.
@AmyNeville Glad it helped. Would you wanna know more information? I mean, do you have any more questions for me?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.