0
\$\begingroup\$

Given seconds like 11728, write the smallest javascript function that returns a string like 3hrs. 15min. 28sec.

signature textTime([int] t,[bool] z,[bool] m)

t is the time like 11728 and you can assume int
z is optional and true means drop zero times, so 3hrs. 0min. 28sec. becomes 3hrs. 28sec.
m is optional and true means show only up to minutes, so 3hrs. 16min. 48sec. is 3hrs. 16min.

For 1 hour, 1hrs. is fine (vs 1hr.)

Example:

textTime(11728) returns "3hrs. 0min. 28sec."
textTime(11728,true) returns "3hrs. 28sec."
textTime(11728,true,true) returns "3hrs."

Regex is fine, speed is inconsequential.

Shortest code wins.

\$\endgroup\$

closed as unclear what you're asking by Johannes Kuhn, Doorknob, Paul Prestidge, J B, Toto Sep 23 '13 at 12:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    \$\begingroup\$ Your function parameters don't make any sense. \$\endgroup\$ – Shmiddty Sep 12 '13 at 18:14
  • 1
    \$\begingroup\$ What about days, weeks, months and years? Examples don't make a specification. \$\endgroup\$ – Johannes Kuhn Sep 13 '13 at 7:43
  • \$\begingroup\$ this is not "date time", but a measure of hours and minutes and seconds. 1 day is not exactly 24hours and 0 secs, so a lot of things are measured in hours, which is the highest level measure of "real" time \$\endgroup\$ – AwokeKnowing Sep 13 '13 at 19:10
  • \$\begingroup\$ I apologize for using [brackets] to state the var type, which made it look like an array. maybe [int] t would have been better. \$\endgroup\$ – AwokeKnowing Sep 13 '13 at 19:20
  • \$\begingroup\$ jsfiddle.net/CA6GM/2 "Javascript 120" \$\endgroup\$ – cocco May 27 '14 at 12:22
1
\$\begingroup\$

Javascript, 194 192

function T(a,c,e){d=60;s=["sec","min","hrs"];alert(t=[a,(0|a/d)*d,(0|a/d/d)*d*d].map(function(a,b,f){p=(a-(0|f[b+1]))/Math.pow(d,b);return e&&1>b?"":c&&!p?"":p+s[b]+". "}).reverse().join(""))}

Run it e.g. with

T(7215,true,false) //2hrs. 15sec.

Could probably be golfed a bit more
Edit Notes:
Added function name for the cost of 1 character* Renamed the function to T. I'm assigning t in the function hence i was overwriting the function itself. Changed (a/(d*d)) -> (a/d/d)

\$\endgroup\$
  • 1
    \$\begingroup\$ Excellent!, it works in all cases. I'm marking this as the winner because it's current standard javascript. \$\endgroup\$ – AwokeKnowing Sep 13 '13 at 19:31
  • 1
    \$\begingroup\$ t(3600, true) gives TypeError: string is not a function. Also, a/(d*d)a/d/d and a-(0|f[b+1])0|a-f[b+1]. \$\endgroup\$ – Ry- Sep 13 '13 at 23:32
  • \$\begingroup\$ @minitech Thanks! I don't know how i missed the d/d. 1. Only the second time, called. I was overriding t 2.Changed it. 3. This is not quite correct. I'm needing the first one. as I'm accessing f[b+1] which can be undefined when exceeding the arrays range.2-(0|undefined /*0*/) //2 wheras 0|(2-undefined /*NaN*/) //0 \$\endgroup\$ – C5H8NNaO4 Sep 14 '13 at 16:14
  • \$\begingroup\$ Oh, in that case, try ~~f[b+1]. \$\endgroup\$ – Ry- Sep 14 '13 at 19:08
1
\$\begingroup\$

155 147 characters.

textTime=(t,z,m,x)=>(x=[t/3600,t/60%60,t%60].map((t,i)=>(!z||t|0)&&~~t+["hrs","min","sec"][i]+"."||0).slice(0,2+!m),(z?x.filter(x=>x):x).join(" "))

If the parameters made a little more sense, yes, it would be fewer.

Non-ES6, changing the name and using globals like @C5H8NNaO4 did, at 183 characters:

function t(t,z,m){return(x=[t/3600,t/60%60,t%60].map(function(t,i){return(!z||t|0)&&~~t+["hrs","min","sec"][i]+"."||0}).slice(0,2+!m),(z?x.filter(function(x){return x}):x).join(" "))}
\$\endgroup\$
  • \$\begingroup\$ How do i run it ? \$\endgroup\$ – C5H8NNaO4 Sep 13 '13 at 12:45
  • \$\begingroup\$ @C5H8NNaO4: The latest Firefox. \$\endgroup\$ – Ry- Sep 13 '13 at 14:19
  • \$\begingroup\$ those are like c# lamdas right? are they (will they be) standard javascript? \$\endgroup\$ – AwokeKnowing Sep 13 '13 at 19:13
  • \$\begingroup\$ @AwokeKnowing: Yes, they’re part of ES6, the next/“current” standard. You can already use them in Firefox. In other browsers, it’s function t(t,z,m,x){return x=(m?[t/60,t%60]:[t/3600,t/60%60,t%60]).map(function(t,i){return(!z||t|0)&&~~t+["sec","min","hrs"][2-i-~~m]+"."||0}),(z?x.filter(function(x){return x;}):x).join(" ")} (193). \$\endgroup\$ – Ry- Sep 13 '13 at 20:53
  • \$\begingroup\$ I got SyntaxError: Unexpected token ILLEGAL when I just pasted this version into chrome (the console) \$\endgroup\$ – AwokeKnowing Sep 13 '13 at 23:22
1
\$\begingroup\$

165 chars

function x(T, Z, M){
f=Math.round;h=T/3600;m=h%1*60;s=m%1*60;
r=f(h)+'hrs '+f(m)+'min '+f(s)+'sec'
Z?r=r.replace(/0\w+/g,''):0
M?r=r.replace(/\d+sec/,''):0
return r}

I just realized that this solution doesn't work for T<60, so I added a few characters and that fixed it (see below):

167 chars

function x(T, Z, M){
f=Math.round;h=T/3600;m=h%1*60;s=m%1*60;
r=f(h)+'hrs '+f(~~m)+'min '+f(s)+'sec'
Z?r=r.replace(/0\w+/g,''):0
M?r=r.replace(/\d+sec/,''):0
return r}

If you don't care about getting the rounding quite right:

149 chars

function x(T, Z, M){
h=T/3600;m=h%1*60;s=m%1*60;
r=~~h+'hrs '+~~m+'min '+~~s+'sec'
Z?r=r.replace(/0\w+/g,''):0
M?r=r.replace(/\d+sec/,''):0
return r}
\$\endgroup\$
  • \$\begingroup\$ Dat modulus though.. amazing! \$\endgroup\$ – NiCk Newman Nov 1 '15 at 12:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.