2018-10-04 14:37:37 +01:00
---
title: General FizzBuzz
id: 5a23c84252665b21eecc7e78
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302273
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
2019-05-23 13:57:59 +09:00
Write a generalized version of < a href = "https://rosettacode.org/wiki/FizzBuzz" target = "_blank" > FizzBuzz< / a > that works for any list of factors, along with their words.
2018-10-04 14:37:37 +01:00
This is basically a "fizzbuzz" implementation where the rules of the game are supplied to the user. Create a function to implement this. The function should take two parameters.
2019-03-05 14:39:16 +09:00
The first will be an array with the FizzBuzz rules. For example: < code > [ [3, "Fizz"] , [5, "Buzz"] ]< / code > .
2020-02-08 13:29:10 -05:00
This indicates that < code > Fizz< / code > should be printed if the number is a multiple of 3 and < code > Buzz< / code > if it is a multiple of 5. If it is a multiple of both then the strings should be concatenated in the order specified in the array. In this case, < code > FizzBuzz< / code > if the number is a multiple of 3 and 5.
2018-10-04 14:37:37 +01:00
The second parameter is the number for which the function should return a string as stated above.
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2018-10-08 12:52:10 +01:00
- text: < code > genFizzBuzz</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof genFizzBuzz=='function');
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6)</ code > should return a string.
2019-07-26 05:24:52 -07:00
testString: assert(typeof genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6)=='string');
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6)</ code > should return < code > "Fizz"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 6), "Fizz");
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 10)</ code > should return < code > "Buzz"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 10), "Buzz");
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 12)</ code > should return < code > "Buzz"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 12), "Buzz");
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 13)</ code > should return < code > "13"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 13), '13');
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 15)</ code > should return < code > "BuzzFizz"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Buzz"],[5, "Fizz"]], 15), "BuzzFizz");
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 15)</ code > should return < code > "FizzBuzz"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"]], 15), "FizzBuzz");
2018-10-20 21:02:47 +03:00
- text: < code > genFizzBuzz([[3, "Fizz"],[5, "Buzz"],[7, "Baxx"]], 105)</ code > should return < code > "FizzBuzzBaxx"</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.equal(genFizzBuzz([[3, "Fizz"],[5, "Buzz"],[7, "Baxx"]], 105), "FizzBuzzBaxx");
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-05 14:39:16 +09:00
function genFizzBuzz(rules, num) {
2018-10-04 14:37:37 +01:00
// Good luck!
}
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function genFizzBuzz(rules, num) {
2018-10-20 21:02:47 +03:00
let res='';
2018-10-04 14:37:37 +01:00
rules.forEach(function (e) {
if(num % e[0] == 0)
res+=e[1];
})
2018-10-20 21:02:47 +03:00
if(res==''){
2018-10-04 14:37:37 +01:00
res=num.toString();
}
return res;
}
```
< / section >