From d992ec24707848a965ea8e69c517ec0d1fdb6b92 Mon Sep 17 00:00:00 2001 From: ClaraCrazy Date: Thu, 3 Oct 2024 07:25:20 +0200 Subject: [PATCH] Implement convertUp helper function --- application/helper.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/application/helper.js b/application/helper.js index 9441979..4bb2b80 100644 --- a/application/helper.js +++ b/application/helper.js @@ -29,13 +29,30 @@ class Helper { } /** - * Convert time to highest possible unit where i > 2 - * @returns {Date} + * Convert time to highest possible unit (minutes, hours, days) where `time > 2` and `Number.isSafeInteger(time)` (whole number) + * @param {Number} time + * @param {String} unit + * @returns {String} */ convertUp(time, unit) { - // TODO: Implement - return time +` ${unit}` + let convertedTime = time; + let convertedUnit = unit; + + if (convertedUnit === 'minutes') { + if (convertedTime > 120 && Number.isSafeInteger(convertedTime / 60)) { + convertedTime = convertedTime / 60; + convertedUnit = 'hours'; + } + } + + if (convertedUnit === 'hours') { + if (convertedTime > 48 && Number.isSafeInteger(convertedTime / 24)) { + convertedTime = convertedTime / 24; + convertedUnit = 'days'; + } + } + return `${convertedTime} ${convertedUnit}`; } -}; +} module.exports = Helper