jQuery jAM, Season 6

Exercise 11.01

Arrays

	 
topEnv["array"] = function(){
	return Array.prototype.slice.call(arguments, 0);
};

topEnv["length"] = function(array){
	return array.length;
};

topEnv["element"] = function(array, i){
	return array[i];
};

run("do(define(sum, fun(array,",
    "    do(define(i, 0),",
    "      define(sum, 0),",
    "        while(<(i, length(array)),",
    "          do(define(sum, +(sum, element(array, i))),",
    "           define(i, +(i, 1)))),",
    "      sum))),",
    "   print(sum(array(1, 2, 3))))");
// -> 6
	 
	

To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.