- Fizz buzz
- Iterators and generators - JavaScript | MDN
- function* - JavaScript | MDN
- Generator - JavaScript | MDN
- yield - JavaScript | MDN
- Fork and clone this repository.
- Change into the new directory.
- Install dependencies.
- Create and checkout a new branch to work on.
- Fulfill the listed requirements.
Starter code is available in lib/challenge.js
. A pull
request is not required, but it is necessary if you want a code review.
You may wish to refer to FAQs related to forking, cloning.
Fizz Buzz is a game used to teach children division by having them group up and count up from one. If the number is divisible by three, then "fizz" is said instead of the number. Similarly, if the number is divisible by five, then "buzz" is said instead. If the number is divisible by both three and five, then "fizz buzz" is said, and if the number isn't divisible by either three or five, then the number is said.
JavaScript generator functions are special functions that return an iterator
object. The returned iterator object has a method called next()
that returns
an object containing two keys, done
and value
. The value of done
is a
boolean value, true
or false
, that indicates whether the iterator has
finished returning all of its values or not. The value of value
is the
yield
ed value from the iterator. If a finite generator's values have been
exhausted (i.e., done
is true
), then value
will be undefined
. It is
possible to create finite and infinite generators.
Here is an example of a finite generator that takes a string as an input, splits it using whitespace as delimiters, strips out punctuation, converts every word to lowercase, and yields each word on successive iterations.
const wordGenerator = function * (string) {
const words = string.split(/\s+/)
.map(word => word.replace(/\W+/, ''))
.map(word => word.toLowerCase())
for (const word of words) {
yield word
}
}
const words = wordGenerator('Hello, World! This is a finite generator.')
let word = words.next()
do {
console.log(word.value)
word = words.next()
} while (!word.done)
Here is an example of an infinite generator that returns natural numbers on each iteration.
const naturalNumberGenerator = function * () {
let number = 1
while (true) {
yield number++
}
}
const naturalNumbers = naturalNumberGenerator()
let naturalNumber = naturalNumbers.next()
for (let i = 0; i < 10; i++) {
console.log(naturalNumber.value)
naturalNumber = naturalNumbers.next()
}
Write a finite generator function, fizzBuzzGenerator
, that, beginning from
1, yields 'Fizz' if the number is divisible by 3, 'Buzz' if the number is
divisible by 5, 'Fizz Buzz' if the number is divisible by 15, and the number
itself if none of the previous conditions are met. The generator function should
take a parameter, max
, that determines the maximum value that should be
yielded. Assume that all inputs are valid (natural numbers).
You should be running grunt nag
before diagnosing any bugs, since it finds
some of the most common sources of errors. After grunt nag
passes, you should
run grunt test
to run the included tests. Tests will tell you whether or not
you've met these requirements.
Modify fizzBuzzGenerator
so that it acts as an infinite generator if no
argument is passed in. To run the test for this bonus, open
spec/challenge.spec.js
and change xdescribe
to describe
on line 45.
Developers should run these often!
grunt nag
: runs code quality analysis tools on your code and complains.grunt test
: runs any automated tests; may depend ongrunt build
.grunt
: runs bothnag
and thentest
grunt make-standard
: reformats all your code in the standard style.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.