javascript FizzBu​​zz

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript FizzBu​​zz相关的知识,希望对你有一定的参考价值。

// array [1...100]
const arr = Array.from(Array(101).keys()).slice(1)

// fizzBuzz function
const fizzBuzz = ({ number, compare, msg }) => 
  number % compare === 0 ? msg : number

// condition functions
// [...new Set(fns)].sort().pop() の部分の処理が無理やり感があるので改善が必要
const compose = (...fns) => [...new Set(fns)].sort().pop()

arr.map(num => {
  const result = compose(
    fizzBuzz({ number: num, compare: 3, msg: 'Fizz' }),
    fizzBuzz({ number: num, compare: 5, msg: 'Buzz' }),
    fizzBuzz({ number: num, compare: 15, msg: 'FizzBuzz' })
  )
  console.log(result)
})
const arr = Array.from(Array(101).keys()).slice(1)

const fizzBuzz = (number, multiple, message) => number % multiple === 0 ? message : false

// まだわかりにくいが、fizzbuzz.jsより読みやすい気がする
const compose = (fizzbuzz, fizz, buzz) => (number) => fizzbuzz || fizz || buzz || number

arr.map(num => {
  const result = compose(
    fizzBuzz(num, 15, 'FizzBuzz'),    
    fizzBuzz(num, 3, 'Fizz'),
    fizzBuzz(num, 5, 'Buzz')
  )(num)
 
  console.log(result)
})

以上是关于javascript FizzBu​​zz的主要内容,如果未能解决你的问题,请参考以下文章

javascript FizzBu​​zz

C#FizzBu zz交换机解决方案

FizzBu​​zz 使用三元条件运算符

markdown FizzBu​​zz

python FizzBu​​zz.py

python FizzBu​​zz.py