运动> Javascript>汉明运动:未定义不是一个功能'错误

时间:2014-10-29 21:35:59

标签: javascript jasmine

有人能告诉我这里我做错了什么吗?该测试将'compute'函数视为未定义,并且我完全没有想法。

这是我得到的错误:

Failures:

1) Hamming no difference between identical strands
   Message:
     TypeError: undefined is not a function
   Stacktrace:
     TypeError: undefined is not a function
    at null.<anonymous> (C:\Users\Schann\exercism\javascript\hamming\hamming_test.spec.js:6:12)

2) Hamming complete hamming distance for single nucleotide strand
   Message:
     TypeError: undefined is not a function
   Stacktrace:
     TypeError: undefined is not a function
    at null.<anonymous> (C:\Users\Schann\exercism\javascript\hamming\hamming_test.spec.js:10:12)

Finished in 0.041 seconds
2 tests, 2 assertions, 2 failures, 0 skipped

//-------------------
// Hamming.js
//-------------------
var Hamming = function() {
    var compute = function(input1, input2) {
        var diff = 0;
        for (i = 0; i < input1.length; i++) {
            if (input1[i] != input2[i]) {
                diff = diff + 1;            
            };
        };
        return diff;
    };
};

module.exports = Hamming;

//-------------------
// hamming_test.spec.js 
//-------------------

var compute = require('./hamming').compute;

describe('Hamming', function () {

  it('no difference between identical strands', function () {
    expect(compute('A', 'A')).toEqual(0);
  });

  it('complete hamming distance for single nucleotide strand', function () {
    expect(compute('A','G')).toEqual(1);
  });

[rest truncated]

});

1 个答案:

答案 0 :(得分:6)

您的compute函数永远不会被导出,它只是Hamming函数的局部变量。

你想做的更像是:

var Hamming = {
  compute: function(input1, input2) {
    var diff = 0;
    for (i = 0; i < input1.length; i++) {
      if (input1[i] != input2[i]) {
        diff = diff + 1;
      }
    }
    return diff;
  }
};

module.exports = Hamming;

我的猜测是你有一个来自Java和C ++等经典编程语言的背景,并且将Hamming函数视为具有compute成员的类声明。