Skip to content

Instantly share code, notes, and snippets.

@Mikeysax
Last active August 27, 2021 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mikeysax/9849e79ee80e5a98550686e793ceda65 to your computer and use it in GitHub Desktop.
Save Mikeysax/9849e79ee80e5a98550686e793ceda65 to your computer and use it in GitHub Desktop.
myMap.js
// 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