DEV Community

Discussion on: JavaScript Katas: Seconds To Minutes And Hours

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited
const toMinutesAndHours = s=>`${~~(p=s/3600)} hour(s) and ${~~(p%1*60)} minute(s)`;
Enter fullscreen mode Exit fullscreen mode

By the way, you missed the seconds parameter in your function definitions. Running them gives an error.__

Collapse
 
lbermudez profile image
lbermudez

But where is var p declared? Only one line is not posible ;)
It would be:

const toMinutesAndHours = (num) => {
    let p;
    return `${~~(p = num / 3600)} hour(s) and ${~~((p % 1) * 60)} minute(s)`;
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Perfectly possible. There was no rule about not polluting the global namespace. The function works just fine

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Or, if you don't want to pollute globals:

const toMinutesAndHours = (s,p=s/3600)=>`${~~p} hour(s) and ${~~(p%1*60)} minute(s)`
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
lbermudez profile image
lbermudez • Edited

Ok, I executed it on node.js.
The latest solution change the contract of the function.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

So? Still solves problem, and still works. Just showing how it could be written in a short way. Code golf. I'm not advocating for this style in project code.

It still only accepts one parameter (function length is still 1) - nothing in the description of the task says we can't also have an optional parameter 😀

Thread Thread
 
lbermudez profile image
lbermudez

yeah! It's cool, no problem! 😉