Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
# fizzbuzz-redux__W5-A4

Tonight's assignment: refactor your FizzBuzz into a library and publish it to the npm registry.

You will first refactor the object to have an `input` and an `output` method, instead of the "read" and "write" methods.

Just like last night's homework, your constructor should accept arguments that change the strings "Fizz" and "Buzz" (e.g. `new FizzBuzz('Bleep', 'Blorp')`). The `input` method should accept two arguments, `min` and `max`—you may safely assume they are `Numbers`. `output` does not take any arguments, but _must_ return an array.

Your module should expose a function using the CommonJS `module.exports` construct. I should be able to `npm install <your-module>` and use it like so:

```
var FizzBuzz = require('<your-module>');

var buzzer = new FizzBuzz(); // Should accept string arguments that replace 'Fizz' and 'Buzz'
buzzer.input(1, 120);

console.log(buzzer.output());
```

Name your module `sea-d44-fizz-buzz-<your initials>` and publish it to the npm registry. And, as usual, fork this repo and submit a PR.

# sea-d44-fizz-buzz-hl

A simple application that generates the classic FizzBuzz responses for a given set of numbers. The application can accept user provided start, end, and string values to customize the output.
30 changes: 30 additions & 0 deletions generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use strict";

(function() {

function _generateOutput(min, max, strings) {
var string1 = strings[0] || "Fizz";
var string2 = strings[1] || "Buzz";
var outputArray = [];

for(var i = min; i <= max; i++) {
var output = "";

if (i % 3 === 0) {
output = string1;
}
if (i % 5 === 0) {
output += string2;
}
if (!output) {
output = i;
}
outputArray.push(output);
}

return outputArray;
}

module.exports = _generateOutput;

})();
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

(function() {

var _generateOutput = require("./generator.js");

var _stringsArray = [];
var _outputArray;

var FizzBuzz = function(string1, string2) {
_stringsArray.push(string1, string2);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than simply pushing your values in to your _stringsArray, when your constructor is run you might want to also reset your _stringsArray; the current implementation actually keeps it from allowing different instances to have different output. Currently, this is what's happening:

var FizzBuzz = require("sea-d44-fizz-buzz-hl");
var buzz1 = new FizzBuzz();
buzz1.input(1,10);
console.log(buzz1.output()); // >> [1,2,"fizz",4,"buzz",...]

var buzz2 = new FizzBuzz("tick", "tock");
buzz2.input(1,100);
console.log(buzz2.output()); // >> [1,2,"fizz",4,"buzz"...]

Basically, with these private variables, you're sharing your data between the instances (which can be useful in some cases). It might make more sense to store the _stringsArray as a property of the instance: this.stringsArray instead:

var FizzBuzz = function(string1, string2) {
  this.stringsArray = [string1, string2];
};

}

FizzBuzz.prototype = {
input: function(min, max) {
_outputArray = _generateOutput(min, max, _stringsArray);
},
output: function() {
return _outputArray;
}
}

module.exports = FizzBuzz;

})();
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "sea-d44-fizz-buzz-hl",
"version": "1.0.0",
"description": "A simple application that generates the classic FizzBuzz responses for a given set of numbers.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/hollislau/fizzbuzz-redux__W5-A4.git"
},
"keywords": [
"sea-d44"
],
"author": "Hollis Lau <[email protected]>",
"license": "ISC",
"bugs": {
"url": "https://github.com/hollislau/fizzbuzz-redux__W5-A4/issues"
},
"homepage": "https://github.com/hollislau/fizzbuzz-redux__W5-A4"
}