peterschussheim
10/17/2016 - 6:31 PM

using map, reduce and filter

using map, reduce and filter

// given an array of numbers, return a new array
// of each element rounded up to the nearest whole integer.

var decimals = [1.5, 2.56, 5.1, 12.33];

function roundUp(arr) {
    return arr.map(Math.ceil);
}

roundUp(decimals)
var merge = function merge(target) {
    var sources = [].slice.call(arguments, 1);
    sources.forEach(function(source) {
        var sourcePropNames = Object.getOwnPropertyNames(source);
        sourcePropNames.forEach(function(propName) {
            Object.defineProperty(target, propName, Object.getOwnPropertyDescriptor(propName, source));
        });
    });
    return target;
}

merge({a: 1}, {})
var defaults = function defaults(target) {
    var sources = [].slice.call(arguments, 1);
    sources.forEach(function(source) {
        if (source) {
            var prop;
            for (prop in source) {
                if (target[prop] === undefined) {
                    target[prop] = source[prop];
                }
            }
        }
    });
    return target;
}
// take an array of strings in the formt "MM-DD"
// and transform each element to format "MM/DD"
// returning a new array of strings.

var bdays = ['08-14', '10-04', '04-21']; 

function convertDateFormat(arr) {
    return arr.map(e => e.replace('-', '/'));
}

convertDateFormat(bdays);

using map, reduce and filter

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.