LoginSignup
1
0

More than 3 years have passed since last update.

TypeScriptでFizzBuzzしてみた

Posted at

TypeScriptでFizzBuzzがしたいと思い立ち、やってみた。

環境設定

npm init
npm install typescript -s
npm run tsc -- --init
npm install lodash
npm install --save @types/lodash

関数型で書きたいのでlodashもインストール。

実装

import * as _ from "lodash";

const ori = _.range(1,100)
const ret = ori.map(fizzbuzz)

ret.map((val)=>console.log(val))

function fizzbuzz(val: number) {
    if((val % 3 == 0) && (val % 2 == 0)){
        return "fizzbuss"
    }else if(val % 3 == 0){
        return "fizz"
    }else if(val % 2 == 0){
        return "buzz"
    }else{
        return String(val)
    }
}

関数型では変数はconstで作れ、とのことなのでconstで定義。

実行

tsc app.ts
node app.js

所感

  • 以下の辺りが、もっときれいに書ける気がするが知識不足
    const ret = ori.map(fizzbuzz)
    ret.map((val)=>console.log(val))

  • console.logを使用すると非純粋になってしまうので、
    モナド?を使いたいが知識不足

分かる人いたらコメントください

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0