General FizzBuzz: Difference between revisions

→‎ES6: Remove outer IIFE and Hungarian notation, use array destructuring, put range above fizzBuzz, trim extra \n, switch args to allow default rules, add default rules, reformat
m (→‎{{header|Scala}}: Fix indentation)
(→‎ES6: Remove outer IIFE and Hungarian notation, use array destructuring, put range above fizzBuzz, trim extra \n, switch args to allow default rules, add default rules, reformat)
Line 1,337:
===ES6===
 
<lang JavaScript>(()// =range :: Int -> Int -> {[Int]
const range = (mmin, nmax) =>
Array.from({ length: max - min }, (_, i) => mmin + i);
 
const defaultRules = Object.freeze([
// fizz :: [[Int, String]] -> Int -> String
[3, 'Fizz'],
const fizz = (lstRules, lngMax) => range(1, lngMax)
[5, 'Buzz'],
.reduce((strSeries, n) =>
[7, 'Baxx'],
])
 
// fizzfizzBuzz :: Int -> [[Int, String]] -> Int -> String
// The next member of the series of lines:
const fizzBuzz = (max, rules = defaultRules) =>
// a word string or a number string
range(1, max + 1).reduce((strSeriesresult, n) =>
strSeries + (
result + (
lstRules
rules.reduce((strwords, [factor, tplNumWordword]) =>
words + (n % factor ? '' : word), str + (''
) || );n
n % tplNumWord[0] ? '' : tplNumWord[1]
) + '\n', ),''
).slice(0, -1)
''
) || n.toString()
) + '\n', ''
);
 
// range :: Int -> Int -> [Int]
const range = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
 
 
return fizz([
[3, 'Fizz'],
[5, 'Buzz'],
[7, 'Baxx']
], 20);
 
})();</lang>
 
console.log(fizzBuzz(20))</lang>
 
{{Out}}
<pre>1
1
2
Fizz