Exercise> Javascript> Hamming Exercise: Getting <undefined is not a function "

Can someone tell me what I'm doing wrong here? The test treats the "compute" function as undefined, and I completely rule out why.

Here is the error I get:

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]

});
+4
source share
1 answer

Your function is computenever exported, it is only a local variable of the function Hamming.

What you want to do is more like:

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;

I assume that you have a background from a classic programming language such as Java and C ++, and see your function Hammingas a class declaration with a member compute.

+6

All Articles