如何比较两个javascript数组并计算差异

时间:2014-01-05 18:59:09

标签: javascript arrays count compare

我正在研究一个exercism.io,找不到比较两个javascript数组并计算差异的可靠答案。这是一个有效的例子;我希望将来可以帮助某人!

1 个答案:

答案 0 :(得分:0)

var DNA = function(input){
  this.input = input;

  this.hammingDistance = function(letters){
    var split_input = input.split(''),
    split_letters = letters.split(''),
    length = Math.min(input.length, letters.length),
    countNotMatched = 0;

    for (var i = 0; i < length; i++){
      if (split_letters[i] !== split_input[i]){
        countNotMatched = countNotMatched + 1;
      }
    }
    return countNotMatched;
  };
};