Question: write 1 function to handle

sum(1,2)
sum([1,2])
sum(1)(2)

Answer (use Drupal coding standards)

/* Purpose:
 *   Write 1 function for calls:
 *     sum(1,2)
 *     sum([1,2])
 *     sum(1)(2)
 * Return:
 *   1 int, either number or String or primitive type
 */
function sum() {
  switch(arguments.length) {
    case(1):
      if(typeof(arguments[0])=="number") {
        var tmp_sum = arguments[0];
        function tmp_f(a) {
          tmp_sum += a;
          return tmp_f;
        }
        tmp_f.toString = function(){ return tmp_sum; }
        return tmp_f;
      }
      else
        if(Array.isArray(arguments[0])) {
          return arguments[0][0]+arguments[0][1];
        }
        else { return "Error: 1 argument is neither int nor array"; }
      break;
    case(2):
      return arguments[0]+arguments[1];
      break;
    default:
      return "Error: incorrect number of arguments";
      break;
  }
}