Created
December 1, 2011 20:07
-
-
Save mrmarcondes/1419452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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