From 1c25e9b03ee4f6b436c4a993a7f1fc82c271ea4f Mon Sep 17 00:00:00 2001 From: koronya Date: Wed, 18 Mar 2026 05:11:37 +0900 Subject: [PATCH] [JS][7kyu] Get decimal part of the given number --- .../koronya.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 codewars/7kyu/get-decimal-part-of-the-given-number/koronya.js diff --git a/codewars/7kyu/get-decimal-part-of-the-given-number/koronya.js b/codewars/7kyu/get-decimal-part-of-the-given-number/koronya.js new file mode 100644 index 000000000..47f57ffa7 --- /dev/null +++ b/codewars/7kyu/get-decimal-part-of-the-given-number/koronya.js @@ -0,0 +1,14 @@ +// [JS][7kyu] Get decimal part of the given number +// get-decimal-part-of-the-given-number +// https://www.codewars.com/kata/586e4c61aa0428f04e000069/train/javascript + +const getRoundNumber = (num, n) => Math.round(num * 10 ** n) / 10 ** n + +const getDecimal = (n) => { + const afterDotLength = String(n).split('.')[1]?.length || 0 + return getRoundNumber(Math.abs(n) % 1, afterDotLength) +} + +getDecimal(10) === 0 +getDecimal(-1.2) === 0.2 +getDecimal(4.5) === 0.5