DEV Community

Dexty Genius
Dexty Genius

Posted on

How do you write a javascript function for this problem

From an integer X representing a time duration in seconds produce a simplified string representation.

For example, given:
100
You should output:
"1m40s"

Use the following abbreviations w,d,h,m,s to represent:

  • 1w is 1 week
  • 1d is 1 day
  • 1h is 1 hour
  • 1m is 1 minute
  • 1s is 1 second

Only the two largest non-zero units should be used. Round up the second unit if necessary so that the output only has two units even though this might mean the output is for slightly more time than X seconds.

Write a function:

function solution(X);

that, given an integer X, returns a string representing the duration.

Examples:

  1. Given X=100, return "1m40s"
  2. Given X=7263, return "2h2m". (7263s=2h1m3s, but this uses too many units, so we round the second largest unit up to 2h2m)
  3. Given X=5, return "5s"

Top comments (0)