Javascript Array myMap(callback)
var s = [23, 65, 98, 5]; Array.prototype.myMap = function(callback) { var newArray = []; s.forEach(a => {//from ww w . j ava2 s . c o m newArray.push(callback(a)); }); return newArray; }; var new_s = s.myMap(function(item) { return item * 2; }); console.log(new_s);
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Map Array.prototype.myMap = function(callback){ var result =[]; for(var i=0; i<this.length; i++){ result.push(callback(this[i]));//from www . j a va 2s . co m } return result; } /* Best if you don't code out here. If you want to check your code, use `index.js`! */
// implement the Array.prototype.map function in JavaScript (es6) // a for loop should suffice, iterate from 0 to the length of the // array - 1 and call the function on it Array.prototype.myMap = function(callback) { let mapped = []; for (let i = 0; i < this.length; i++) { mapped.push(callback(this[i]));//from w w w . j a v a 2 s .co m } return mapped; }; console.log([1,2,3,4,5].myMap( (el) => Math.pow(el, 2)) ); console.log([].myMap( (el) => Math.pow(el, 2)) ); console.log([1,2,3,4,5].myMap( (el) => el));
// filter - Implement your owen Array.prototype.map function // Solution - 1// ww w . ja va 2 s .c o m Array.prototype.myMap = function (callback) { arr = [] for (let i = 0; i < this.length; i++) { arr.push(callback(this[i], i, this)); } return arr; } let numbers = [ 1, 4, 9 ] ; let squareRoot = numbers.myMap(i => Math.sqrt(i)) console.log(squareRoot); // => [ 1, 2, 3 ] // Without using Array.prototype myMap2 = (array, callback) => { let arr = []; for (let i = 0; i < array.length; i++) { arr.push(callback(array[i], i, this)) } return arr; } let squareRoot1 = myMap2(numbers, (num) => { return Math.sqrt(num) }) console.log(squareRoot1); // => [ 1, 2, 3 ]
Array.prototype.myMap = function(callBack) { const newArr = [];/*from w w w . ja v a 2s. c om*/ this.myEach((el) => { newArr.push(callBack(el)); }); return newArr; }; // console.log([1,2,3,4,5].myMap((el) => { // return el * 3; // }));
Array.prototype.myMap = function (callback) { var result = []; this.myEach(function(el) { result.push(callback(el));//ww w . ja v a2 s . c o m }); return result; } var b = a.myMap(function (el) { return el * 2; }) console.log(b);
Array.prototype.myMap = function(callback) { var newArray = [] for (var i = 0, max = this.length; i < max; i++) { newArray.push(callback(this[i])) }//w w w. jav a 2 s.c o m return newArray } function upperNames(value) { return value.toUpperCase() } var newUpper = names.myMap(upperNames) console.log(newUpper)
//Map Prototype/*w w w . j a v a 2 s. c o m*/ Array.prototype.myMap = function(callback) { var newArray = []; for(var i = 0; i < this.length; i++) { newArray.push(this[i].toUpperCase()); } return newArray; }; var mapArray = names.myMap(); console.log("Map Prototype: " + mapArray);
function isEven(n) { if (n === 0) {/*from ww w.j a v a 2 s . c om*/ return true; } else if (n === 1) { return false; } else { return isEven(n - 2); } } isEven(5); Array.prototype.myMap = function(callback) { arr = []; for (var i = 0; i < this.length; i++) { arr.push(callback(this[i])); } return arr; } var doubled = [1,2,3].myMap(function(n) { return n ** 2; }); console.log(doubled); function rot13(str) { // LBH QVQ VG! var splitStr = str.split(''); for (var i = 0; i < splitStr.length; i++) { if (splitStr[i] !== ' ') { splitStr[i] = splitStr[i].charCodeAt() + 13; if (splitStr[i] > 90) { splitStr[i] -= 26; } splitStr[i] = String.fromCharCode(splitStr[i]); } } return splitStr.join(''); } // Change the inputs below to test console.log(rot13("SERR PBQR PNZC"));