Skip to content

Instantly share code, notes, and snippets.

@if1live
Created January 3, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save if1live/ee6b51331258a5500986cfa5b30c2810 to your computer and use it in GitHub Desktop.
Save if1live/ee6b51331258a5500986cfa5b30c2810 to your computer and use it in GitHub Desktop.
/*
오늘의 코딩 퀴즈
숫자를 기수로 변환하기
1 => 일
73 => 칠십삼
598 => 오백구십팔
1920 => 일천구백이십
범위는 0부터 100억 미만의 자연수
https://twitter.com/_danuel_/status/1080508715051081728
*/
var convert = (target) => target.toString().split('')
.map((num, idx) => ({num: num-'0', idx, rev_idx: target.toString().length-1-idx}))
.map((x) => ({...x, han: ' 일이삼사오육칠팔구'[x.num]}))
.map((x) => ({...x, base: ' 십백천'[x.rev_idx % 4]}))
.map((x) => ({...x, base: x.han === ' ' ? ' ' : x.base}))
.reverse()
.reduce((acc, cur) => cur.rev_idx % 4 !== 0
? [...acc, cur]
: [...acc, {han: '', base: ' 만억'[Math.floor(cur.rev_idx/4)]}, cur], [])
.reverse()
.map((x) => `${x.han}${x.base}`.trim())
.join('');
console.log(convert(1))
console.log(convert(73))
console.log(convert(598))
console.log(convert(1920))
console.log(convert(123045060789))
/*
칠십삼
오백구십팔
일천구백이십
일천이백삼십억사천오백육만칠백팔십구
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment