The advent of JavaScript has made it really easy to write web pages that interact with you. Really, it's insanely easy. The barrier to entry: way too low. It's not that all JavaScript is bad, just that most JavaScript is not written with simplicity, correctness, consistency, or completeness in mind.

The worst part is that many "Coder 2.0s" don't have a background in other languages so they don't know what sorts of functions to expect from their language. Maybe that's why I don't feel any sympathy for Piotrek, who was looking for calendar code on the web and found this really fantastic way to detect leap years.

 

  div = year / 4;
  str = new String( div );
  var dec = str.indexOf(".");
  if ( dec != -1 )
      { feb_end = 28; }
  else
      { feb_end = 29; }

 

Now, modulo abuse is not uncommon. That's why I'm throwing in (for free) the following code --- discovered by Rob --- which (he says) is supposed to figure out (in seconds) how long an upload took.

 

  function uploadTime(bytes)
  {
      var secs = bytes / 3500;

      if ( secs < 59 )
      {
          return secs.toString().split(".")[0] + " seconds";
      }
      else if ( secs > 59 && secs < 3600 )
      {
          var mints = secs / 60;
          var remainder = parseInt(secs.toString().split(".")[0]) -
(parseInt(mints.toString().split(".")[0]) * 60);
          var szmin;
          if ( mints > 1 )
          {
              szmin = "minutes";
          }
          else
          {
              szmin = "minute";
          }
          return mints.toString().split(".")[0] + " " + szmin + " " +
remainder.toString() + " seconds";
      }
      else
      {
          var mints = secs / 60;
          var hours = mints / 60;
          var remainders = parseInt(secs.toString().split(".")[0]) -
(parseInt(mints.toString().split(".")[0]) * 60);
          var remainderm = parseInt(mints.toString().split(".")[0]) -
(parseInt(hours.toString().split(".")[0]) * 60);
          var szmin;
          if ( remainderm > 1 )
          {
              szmin = "minutes";
          }
          else
          {
              szmin = "minute";
          }
          var szhr;
          if ( remainderm > 1 )
          {
              szhr = "hours";
          }
          else
          {
              szhr = "hour";
          }
          return hours.toString().split(".")[0] + " " + szhr + " " +
mints.toString().split(".")[0] + " " + szmin;
      }
  }

 

After finding this, he rewrote the function in ActionScript but can't get it included in the project. For completeness, here's the new and improved version.

 

  private function getTime(nSize:Number):String{
    var nSeconds:Number = nSize / 3500;
    if (nSeconds < 59) return (Math.ceil(nSeconds) + " second(s)");
    else if (nSeconds < 3599) return ((Math.floor(nSeconds / 60)) + "
minute(s) " + Math.ceil(nSeconds % 60) + " second(s)");
    else return ((Math.floor(nSeconds / 3600)) + " hour(s) " +
Math.ceil((nSeconds % 3600)/60) + " minute(s)");
  }

 

On a serious note: if I hear one more person talk about AJAX without being able to tell me what it means, I'll snap.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!