Last active
August 27, 2021 18:04
-
-
Save Mikeysax/9849e79ee80e5a98550686e793ceda65 to your computer and use it in GitHub Desktop.
myMap.js
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
// Write a function called `myMap` that accepts one argument and a callback. | |
// The argument is either a Object or an Array. | |
// | |
// When the argument is an Array, it applies the callback to each element of the Array | |
// and returns a new Array. | |
// | |
// When the argument is a Object, it applies the callback to each key/value pair of the Object. | |
// expects a Object and returns a new Object. | |
// | |
// Implement this behavior without using Array.map or related methods, such as reduce or filter, which return new Arrays | |
////// | |
// | |
// Examples: | |
////// | |
// For Arrays: | |
// In this example, the method accepts an array and a callback. The callback takes an element and multiplies it by 2. | |
myMap([1, 2, 3], function(value) { | |
return value * 2; | |
} | |
// Desired output: | |
// [2, 4, 6] | |
////// | |
////// | |
// For Objects: | |
// In this example, the method accepts a Object and a callback. The callback takes a key and a value and returns | |
// a new Object with the split name and increased value | |
myMap({ john_johnington: 1000, mike_michaelson: 20, james_jamerson: 50 }, function(key, value) { | |
key = key.split('_')[0]; | |
return { [key]: value += 1000 }; | |
}); | |
// Desired output: | |
// { john: 2000, mike: 1020, james: 1050 } | |
////// | |
////// | |
// Starting Code: | |
function myMap(obj, cb) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment