-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibgolf.js
More file actions
43 lines (41 loc) · 1012 Bytes
/
libgolf.js
File metadata and controls
43 lines (41 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
function FizzBuzz(numbers, config) {
if (numbers && numbers instanceof Array)
config = config;
else {
config = numbers;
numbers = [3, 5];
}
if (!config)
config = {};
if (!config.words)
config.words = config.w || {3:'Fizz', 5:'Buzz'};
if (!config.max_repeats)
config.max_repeats = config.m || 1;
return function(start, end) {
var lines = [];
if (end === undefined)
end = start + 1;
for (var i = start; i <= end; i++) {
var string = '';
for (var key in config.words) {
if (!config.words.hasOwnProperty(key))
continue;
var divisor = parseInt(key),
n = i,
depth = 0;
while (n % divisor === 0 && depth < config.max_repeats) {
string += config.words[key];
n = Math.floor(n / divisor);
depth++;
}
for (var j = 0; j < (config.transforms || '').length; j++)
string = config.transforms[j](string, i);
}
string = string ? string : i;
lines.push(string.toString());
}
return lines;
}
}
var Fz = FizzBuzz;