taesik

Write Number in Expanded Form 본문

functions and trick/typescript_javascript

Write Number in Expanded Form

taesikk 2022. 1. 25. 12:34
function expandedForm(num) {
  return String(num)
          .split("")
          .map((num, index, arr) => num + "0".repeat(arr.length - index -1 ))
          .filter((num) => Number(num) != 0)
          .join(" + ")
}
const expandedForm = n => n.toString()
                            .split("")
                            .reverse()
                            .map( (a, i) => a * Math.pow(10, i))
                            .filter(a => a > 0)
                            .reverse()
                            .join(" + ");

'functions and trick > typescript_javascript' 카테고리의 다른 글

Sum of numbers from 0 to N  (0) 2022.01.25
how many times father see kid's falling and bouncing ball  (0) 2022.01.25
alpabet war  (0) 2022.01.25
isAscending  (0) 2022.01.25
diamond with *  (0) 2022.01.24