tags:

views:

107

answers:

3

Hi there.

I know there is a more readable way of writing this:

var string = ( 
  (r.d != 0 ? r.d + (r.d == 1 ? ' day' : ' days') : '' ) +
  (r.h != 0 ? r.h + (r.h == 1 ? ' hour' : ' hours') : '' ) +
  (r.m != 0 ? r.m + (r.m == 1 ? ' minute' : ' minutes') : '' ) +
  (r.s != 0 ? r.s + (r.s == 1 ? ' second' : ' seconds') : '' ));
+2  A: 

I would use something like this:

function pluralize(aVal, aSuffix) {
   var r = '';

   if (aVal != 0) {
      r = aVal + ' ' + aSuffix + (aVal > 1 ? 's' : '');
   }

   return r;
}

var string = pluralize(r.d, 'day') + pluralize(r.h, 'hour') + 
     pluralize(r.m, 'minute') + pluralize(r.s, 'second')

EDIT: renamed the function to pluralize

RC
+1 Much better than mine.
Kyle Rozendo
Wanted to offer the same solution. Another +1.
spbfox
the name of the function should not be hourify, its a bit confusing..
Sairam Kunala
Here's a minified version of the function `hourify` function: `function h(v,s){return v!=0?v+' '+s+(v>1?'s':''):'';}​` (56 bytes)
NullUserException
The function does much more than pluralizing, it is creating the string. I like the minified version. I feel that one should be converting a string to a number and checking if its between the 0 and 60 or 0 and 24 for it to return the correct value. I am not sure if its a good idea to discuss edge cases here.
Sairam Kunala
A little nitpicking @NullUserEception. As 0 is false in JavaScript you don't need to check if v != 0. A simple v?dosomething:'' is enough.
eskimoblood
+6  A: 

Try something a bit more readable:

function singleOrPlural(val, single, plural){
  if(val == 1)
    return val + ' ' + single;
  if(val > 1)
    return val + ' ' + plural;
  return '';
}

Use as:

singleOrPlural(r.day, 'day', 'days')

You can even go nuts and add this to the prototype, and end up with r.days.singleOrPlural('day', 'days'), but I don't think it's needed here.

Kobi
+1 because it's cleaver regarding i18n for language where plural is not word + s like German for example
RC
@RC - even in English, you have `children`, `cries`, `men` - too many exceptions to just throw an `s` at the end... As for internalization, some languages have even more complex rules. For example, in Hebrew it is common to have a word for "two" (similar to `once` `twice` `3 times`). It is also possible to add a word for "none" other than `''`.
Kobi
nice!, please use the "triple" equivalence operator "===" in the future.
gath
@gath - Thanks. I thought about `===`, but what would I do with `val > 1` in that case? It would create an inconsistency between `('1', 'cow', 'cows') => fail` and `('2', 'cow', 'cows') => works`.
Kobi
A: 
function getvalue(val, unit) 
{
        if(val>0) 
            return val + unit + (val > 1 ? 's' : '');
        else
            return '';
}

var string = gets(r.d, 'day') + gets(r.h, 'hour') + gets(r.m, 'minute') + gets(r.s, 'second');
Anil
This always adds `s`, and doesn't add a space between the unit and the number. Also, it is somewhat weird that it doesn't add the number to its output, you have to do it 'manually'...
Kobi
cool...edited..
Anil