Created
May 20, 2015 20:39
-
-
Save fish2000/5fb7950c64602381d291 to your computer and use it in GitHub Desktop.
Variantions On The Classic FizzBuzz Function In (Relatively) Pure JavaScript, As Prescribed By Mr. On Software, For Demonstrative Purposes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Write a function, `fizzBuzz`, that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". | |
| */ | |
| // Write your implementation here | |
| function fizzBuzz() { | |
| for (var i = 1; i < 101; i++) { | |
| if ((i % 3 == 0) && (i % 5 == 0)) { | |
| console.log("FizzBuzz"); | |
| } else if (i % 3 == 0) { | |
| console.log("Fizz"); | |
| } else if (i % 5 == 0) { | |
| console.log("Buzz"); | |
| } else { | |
| console.log(""+i); | |
| } | |
| } | |
| } | |
| //fizzBuzz() // prints fizzbuzz 1-100... | |
| /* | |
| Write a function that creates a new Object 'FizzBuzzer' that implements `fizzBuzz` from 1 to `max`. `max` should be an "own property" of the created object. | |
| */ | |
| // Write your implementation here: | |
| var FizzBuzzer = function (max_) { | |
| this.max = parseInt(max_, 10); | |
| this.fizzBuzz = function () { | |
| for (var i = 1; i < this.max+1; i++) { | |
| if ((i % 3 == 0) && (i % 5 == 0)) { | |
| console.log("FizzBuzz"); | |
| } else if (i % 3 == 0) { | |
| console.log("Fizz"); | |
| } else if (i % 5 == 0) { | |
| console.log("Buzz"); | |
| } else { | |
| console.log(""+i); | |
| } | |
| } | |
| }; | |
| } | |
| /*var fb = new FizzBuzzer(100) | |
| console.log(fb.hasOwnProperty('max')) // true | |
| console.log(fb.max) // 100 | |
| fb.fizzBuzz() // prints fizzbuzz 1-100...*/ | |
| /* | |
| Write a function that creates a new Object 'FizzBuzzer' that implements `fizzBuzz` from 1 to `max`. `max` should be *private* to the created object and default to `100`. The Object should also implement `getMax` and `setMax` | |
| */ | |
| // Write your implementation here: | |
| var FizzBuzzer = function (max_) { | |
| var max = parseInt(max_, 10); | |
| this.getMax = function () { | |
| return max; | |
| }; | |
| this.setMax = function (max_) { | |
| max = parseInt(max_, 10); | |
| }; | |
| this.fizzBuzz = function () { | |
| for (var i = 1; i < max+1; i++) { | |
| if ((i % 3 == 0) && (i % 5 == 0)) { | |
| console.log("FizzBuzz"); | |
| } else if (i % 3 == 0) { | |
| console.log("Fizz"); | |
| } else if (i % 5 == 0) { | |
| console.log("Buzz"); | |
| } else { | |
| console.log(""+i); | |
| } | |
| } | |
| }; | |
| } | |
| var fb = new FizzBuzzer(100) | |
| console.log(fb.hasOwnProperty('max')) // false | |
| console.log(fb.max) // undefined | |
| console.log(fb.getMax()) // 100 | |
| fb.setMax(99) | |
| fb.fizzBuzz() // prints fizzbuzz 1-99... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment