Skip to content

Instantly share code, notes, and snippets.

View daronwolff's full-sized avatar
🏠
Working from home

daronwolff daronwolff

🏠
Working from home
  • Me
  • México
View GitHub Profile
@daronwolff
daronwolff / array_inverter.js
Last active May 19, 2016 18:10
Javascript - Invert array position elements manually. I created this little code to my friends in Kwan
"use strict";
if (!Array.prototype.reverseOrder) {
Array.prototype.invertir = function() {
var inverted = [];
for (var i = this.length - 1; i >= 0; i--) {
inverted[this.length - (i + 1)] = this[i];
}
return inverted;
}
}
@daronwolff
daronwolff / isArray.js
Created May 19, 2016 23:05
This little script is used to test if an array is really an array or not.
"use strict";
if (Array.prototype.isArray === undefined) {
Array.prototype.isArray = function (value) {
return Object.prototype.toString.apply(value) === "[object Array]";
};
}
/*
Examples
*/
Array.isArray({}); //false
@daronwolff
daronwolff / factorial.js
Created May 20, 2016 16:41
Javascript Factorial function
function factorial(n) {
"use strict";
var result = 1;
while (n > 1) {
result *= n;
console.log(result);
n -= 1;
}
}
@daronwolff
daronwolff / exercise_2.js
Last active May 20, 2016 19:29
Write two binary functions "add" and "mul", that take two numbers and return their sum and product
"use strict";
var add = (function(a){
return function (b){
return a + b;
}
});
var mul = (function(a){
return function(b){
var total = a * b;
return total;
@daronwolff
daronwolff / exercise_3.js
Created May 20, 2016 19:34
Write a function that takes an argument and returns a function that returns that argumen
"use strict";
function identify(x) {
return function(){
return x;
}
}
var idf = identify(3);
idf(); // 3
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;
@daronwolff
daronwolff / exercise_5.js
Created May 20, 2016 21:07
Write a function that takes a function and an argument, and returns a function that can supply a second argument add3 = curry(add,3); add3(4); // 7
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;
@daronwolff
daronwolff / exercise_6.js
Created May 20, 2016 22:03
Write a methodize, a function that converts a binary function to a method
function add(a,b) {
return (a + b);
}
function methodize(func) {
return function(y){
return func(this,y);
}
}
Number.prototype.add = methodize(add);
(2).add(3) // 5
@daronwolff
daronwolff / exercise_7.js
Last active May 20, 2016 22:15
Write a DEmethodize, a function that converts a method to a binary function Example demethodize(Number.prototype.add)(5)(6) // 11
function demethodize(func){
return function(that,y){
return func.call(that,y);
}
}
var add = (function(a) {
return function(b) {
return a + b;
}
});
@daronwolff
daronwolff / exercise_8.js
Created May 20, 2016 22:31
Write a TWICE, function that takes a binary function and returns a unitary function that passes its arguments to the binary function twice Example var double = twice(add); double(11); // 22 var square = twice(mul); square(11); // 121
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;
return total;
}