Skip to content

Instantly share code, notes, and snippets.

@mrmarcondes
Created December 1, 2011 20:07
Show Gist options
  • Save mrmarcondes/1419452 to your computer and use it in GitHub Desktop.
Save mrmarcondes/1419452 to your computer and use it in GitHub Desktop.
var a = [1,2,3,4,5,6,7,8];
Array.prototype.map = function(callback) {
var mapped = [];
for (var i = 0; i < this.length; i++) {
mapped[i] = callback.call(null, this[i]);
}
return mapped;
};
function doubleNumber(n) {
return n * 2;
}
function addOne(n) {
return n + 1;
}
// calcula o quadrado de um numero
function square(n) {
return n * n;
}
// verifica se o numero e par
function isEven(n) {
return (n % 2 === 0) ? true : false;
}
// retorna o tamanho do array
Array.prototype.count = function() {
return this.length;
}
// exclui um determinado objeto do array
Array.prototype.delete = function(object) {
var newArray = [];
for (var i = 0; i < this.length; i++) {
if (this[i] !== object) {
newArray.push(this[i]);
}
}
return newArray;
}
console.log("doubleNumber + addOne: " + a.map(doubleNumber).map(addOne));
console.log("array.square: " + a.map(square));
console.log("doubleNumber + addOne + square: " + a.map(doubleNumber).map(addOne).map(square));
console.log("array.isEven: " + a.map(isEven));
console.log("array.count: " + a.count());
console.log("array.delete: " + a.delete(3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment