Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Created February 10, 2015 21:50
Show Gist options
  • Select an option

  • Save mecampbellsoup/18627d5aa85ff87c14e8 to your computer and use it in GitHub Desktop.

Select an option

Save mecampbellsoup/18627d5aa85ff87c14e8 to your computer and use it in GitHub Desktop.
var anagram = require('./anagram');
describe('Anagram', function() {
it("no matches",function() {
var subject = anagram("diaper");
var matches = subject.matches([ "hello", "world", "zombies", "pants"]);
expect(matches).toEqual([]);
});
};
@mecampbellsoup
Copy link
Copy Markdown
Author

@scottluptowski, how can I make a JS object that can be initialized with an argument (in the above example that's the anagram("diaper") bit) and subsequently that object responds to another method matches which introspects the object's property where we stored the string "diaper" initially?

@mecampbellsoup
Copy link
Copy Markdown
Author

function Anagram (input) {
  this.input = input;
  this.matches = function () {
    // logic goes here...
  };
}

var anagram = function(input) {
  return new Anagram (input);
};

module.exports = anagram;

@sammylupt
Copy link
Copy Markdown

Nice! That's most of the way there.

The better approach to matches is to put on the prototype of the function.

function Anagram (input) {
  this.input = input;
}

Anagram.prototype.matches = function () {
    // logic goes here...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment