@anonymous/

MeanJauntySpreadsheets

JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function addTogether() {
  var totalArgs = arguments.length;
  var arg = arguments[0];

  if (totalArgs === 1) {
    if (typeof(arg) != "number") {//check if arg is number
      return "undefined";
    }
    //else, return a fn that uses the 1st arg and expects another one, to then add to it
    //i.e. if calling addTogether like this ==> addTogether(2)([3]), the 1st arg is 2, and 2nd arg is [3]
    /*addTogether(2)([3]) looks wierd and we might even say the syntax is wrong,
    but it is made possible if we supply a closure function like the one here*/
    return function (x) {
      if (typeof(x) != "number") {//check if the 2nd arg passed is also a number
        return "undefined";
      }
      return x + arg;
    };
  }

  /*test for 2 arguments*/
  if (totalArgs === 2) {
    if (typeof(arguments[0]) != "number" || typeof(arguments[1]) != "number") {
      return "undefined";
    }
    //else, return the sum of both numbers
    return arguments[0] + arguments[1];
  }
}


// addTogether(2, 3);
// addTogether(2)(3);
// addTogether("http://bit.ly/IqT6zt");
// addTogether(2, "3");
addTogether(2)([3]);
Native Browser JavaScript