Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
fizzbuzz 0.1
/*
* fizzbuzz() object
* I play the fizzbuzz game (http://en.wikipedia.org/wiki/Fizz_buzz)
*
* properties:
* start - integer (optional, defaults to 1)
* finish - integer (optional, defaults to 100)
* modFizz - integer (optional, defaults to 3)
* modBuzz - integer (optional, defaults to 5)
* txtFixx - string (optional, defaults to "fizz")
* txtBuzz - string (optional, defaults to "buzz")
*
* methods:
* play();
*
* Usage:
* example 1:
* fizzbuzz.play(); // uses default values
*
* example 2:
* fizzbuzz.start = 5;
* fizzbuzz.finish = 50;
* fizzbuzz.play(); // plays with 5 - 50, uses default values for other properties
*/
var fizzbuzz = {
start: 1,
finish: 100,
modFizz: 3,
modBuzz: 5,
txtFizz: "fizz",
txtBuzz: "buzz",
play: function(){
for (x = this.start; x <= this.finish; x++) {
var sayThis = "";
if (x % this.modFizz == 0) { sayThis += this.txtFizz; }
if (x % this.modBuzz == 0) { sayThis += this.txtBuzz; }
if (sayThis == "") { sayThis = x; }
console.log(sayThis);
}
}
}
@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

Meets the first criteria (print out numbers 1 - 100).
Fails to print out fizz for multiples of 3.
Fails to print out buzz for multiples of 5.

@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

Meets first criteria of printing out numbers 1 - 100.
Prints out fizz for multiples of 3.
Prints out buzz for multiples of 5.
Does not print out fizzbuzz on a single line for numbers that are multiples of both 3 and 5, though.

@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

At this point, all criteria have been met and it should be left alone.

But there's always someone who wants to make a "new and improved" version to show they are better than everyone else, isn't there? ;)

@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

Shiny! See, if we just use a variable as a flag, we can get rid of all those ugly, chained if else statements. See how elegant this code is? #snark

@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

Hey, if we make this into a function with parameters, we can play this game with any starting and finishing numbers! See?

fizzbuzz(1, 100);
fizzbuzz(1, 100000);
fizzbuzz(-1000, 1000);

Isn't that a great improvement?

moreSnark

@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

But what if we want to play a version of the game that uses numbers other than 3 and 5? Or what if we want to play the game in a foreign language?

Now we are looking at function calls like this:

fizzbuzz(1, 100, 3, 5, "fizz", "buzz"); // Earth
fizzbuzz(-1000, 1000, 2, 13, "molto", "bene"); // Gallifrey

With all those additional required parameters, someone's bound to accidentally omit one (which will throw an "undefined" error) or enter them in the wrong sequence (which means the function will work but likely return unintended results.) Yikes!

@mrandrewmills
Copy link
Author

mrandrewmills commented Feb 27, 2015

We can add a documentation header to our function . . .

but who bothers to read directions any more? #yetEvenMoreSnark

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