fractionConverter

Written on August 13, 2019

문제

Write a function that takes a number as its argument and returns a string that represents that number’s simplified fraction.

Example: toFraction(0.5) === ‘1/2’

Whole numbers and mixed fractions should be returned as irregular fractions

Example: toFraction(3.0) === ‘3/1’

Example: toFraction(2.5) === ‘5/2’

풀이

var toFraction = function(number) {
  let simplifiedFraction = "/";
  let splited = number.toString().split(".");
  if (splited.length < 2) {
    simplifiedFraction = number + simplifiedFraction + 1;
    return simplifiedFraction;
  }
  let bottom = Math.pow(10, splited[1].length);
  let upper = Math.floor(number * bottom);
  const areDividable = (number1, number2, factor) => {
    if (number1 % factor === 0 && number2 % factor === 0) {
      return true;
    } else {
      return false;
    }
  };
  const simplify = (upper, bottom) => {
    let factors = [2, 5];
    let count = 0;
    factors.forEach(f => {
      if (areDividable(upper, bottom, f)) {
        upper = upper / f;
        bottom = bottom / f;
      } else {
        count += 1;
      }
    });
    if (count === factors.length) {
      return upper + simplifiedFraction + bottom;
    } else {
      return simplify(upper, bottom);
    }
  };
  return simplify(upper, bottom);
};

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself