Skip to content

AegisNetLab/human-readable-duration

Repository files navigation

Human Readable Duration Format

JavaScript Node.js Jest Codewars License

JavaScript solution for the Codewars kata Human Readable Duration Format.

Description

The function receives a non-negative integer representing a duration in seconds and returns a human-readable string.

Examples:

formatDuration(62);
// "1 minute and 2 seconds"

formatDuration(3662);
// "1 hour, 1 minute and 2 seconds"

formatDuration(0);
// "now"

Rules

  • If the input is 0, return "now".
  • A year is 365 days.
  • A day is 24 hours.
  • Units must appear from the most significant to the least significant.
  • Zero-value units must not appear in the output.
  • Units must use singular or plural form correctly.
  • Components are separated by ", ", except the final component, which is separated by " and ".

Solution

function formatDuration(seconds) {
  if (seconds === 0) return "now";

  const units = [
    ["year", 365 * 24 * 60 * 60],
    ["day", 24 * 60 * 60],
    ["hour", 60 * 60],
    ["minute", 60],
    ["second", 1]
  ];

  const result = [];

  for (const [name, value] of units) {
    const count = Math.floor(seconds / value);

    if (count > 0) {
      seconds %= value;
      result.push(`${count} ${name}${count > 1 ? "s" : ""}`);
    }
  }

  if (result.length === 1) return result[0];

  return `${result.slice(0, -1).join(", ")} and ${result.at(-1)}`;
}

Project Structure

human-readable-duration/
├── README.md
├── solution.js
├── alternative-solutions.js
├── solution.test.js
├── package.json
├── .gitignore
└── LICENSE

Installation

git clone https://github.com/AegisNetLab/human-readable-duration.git
cd human-readable-duration
npm install

Run Examples

node solution.js

Run Tests

npm test

Codewars Kata

Kata: Human Readable Duration Format
https://www.codewars.com/kata/human-readable-duration-format

Author

AegisNetLab

License

This project is licensed under the MIT License.

About

JavaScript solution for the Codewars "Human Readable Duration Format" kata. Converts seconds into a clean, human-friendly string using years, days, hours, minutes, and seconds with proper formatting and grammar.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors