diff --git a/public/index.html b/public/index.html index c4295988ba3db05c05189fa463abf71985e2c263..c0f9bf5b602d03a1e726528a68e04f53e3eef733 100644 --- a/public/index.html +++ b/public/index.html @@ -113,7 +113,7 @@ </div> <div id="calculate-time-element" style="display: none;"> <div class="title"> - <h2>Estimated Reading Time: <span id="reading-time-element"></span> min</h2> + <h2>Estimated Reading Time(hh:mm:ss): <span id="reading-time-element"></span> hours</h2> </div> <div> Send your feedback on: behira.younes@univ-oran2.dz diff --git a/public/index.js b/public/index.js index 0ee1b49b4c1a4936759eca09de7f0c9eafb8d8a6..ca553ccf5d0e338b6f9598ceca2f643263e85a92 100644 --- a/public/index.js +++ b/public/index.js @@ -139,8 +139,7 @@ function calculateReadingTime() { //x = (inputObj.numberOfCharacters * inputObj.schoolLevel * inputObj.category); let combination = inputObj.schoolLevel + '' + inputObj.category; - console.log(combination); - + let C = inputObj.numberOfCharacters; switch (combination) { @@ -209,11 +208,47 @@ function calculateReadingTime() { break; } - // set value to html element + // get modulo division of x for rounding off till 30 seconds + let xMod60 = x % 60; + if (xMod60 <= 15) { + roundOff = 0; + } + if (xMod60 > 15 && xMod60 <= 30) { + roundOff = 30; + } + if (xMod60 > 30 && xMod60 <= 45) { + roundOff = 30; + } + if (xMod60 > 45) { + roundOff = 60; + } + + // subtract mod seconds and add rounded off seconds + let roundedOffSeconds = x - xMod60 + roundOff; + + // get formatted time in hh:mm:ss format + let formattedTime = getFormattedTime(roundedOffSeconds) + + // set time value to html element var s = document.getElementById('reading-time-element'); - s.innerHTML = x; + s.innerHTML = formattedTime; // display html element which shows time let element = document.getElementById('calculate-time-element'); element.style.display = "block"; } } + +// function getFormatted time +// accepts seconds value and returns in hh:mm:ss format +function getFormattedTime(totalSeconds) { + let hours = Math.floor(totalSeconds / 3600); + totalSeconds %= 3600; + let minutes = Math.floor(totalSeconds / 60); + let seconds = totalSeconds % 60; + + minutes = String(minutes).padStart(2, "0"); + hours = String(hours).padStart(2, "0"); + seconds = String(seconds).padStart(2, "0"); + + return hours + ":" + minutes + ":" + seconds; +}