Functions with unknown amount of args.

Posted on
  • Is this (see listing) possible with espruino javascript.

    function add() {
      var i, len, result = 0;
      for (i = 0; len = arguments.length; i < len; ++i) {
      result += arguments[i];
      }
      return result;
    }
    
    add(3, 7, 4, 6);  //result = 20
    
  • Hi,

    Yes, it is. It was only added within the last month or two though. I've just discovered that prefix increment isn't in there yet (and I think you have a typo after 'i=0'), however the following code works fine:

    function add() {
      var i, len, result = 0;
      for (i = 0, len = arguments.length; i < len; i++) {
      result += arguments[i];
      }
      return result;
    }
    add(3, 7, 4, 6);  //result = 20
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Functions with unknown amount of args.

Posted by Avatar for user6155 @user6155

Actions