// sort
{
    var persons = [
        { name: "Иван", age: 17 },
        { name: "Мария", age: 35 },
        { name: "Алексей", age: 73 },
        { name: "Яков", age: 12 },
    ]

    function sort(arr, type, bool = true) {
        return arr.sort((a, b) => {
            let result;
            if (bool) {
                console.log('true')
                result = a[type] > b[type] ? 1 : -1;
            } else {
                console.log('false')
                result = a[type] < b[[type]] ? 1 : -1;
            }
            return result
        })
    }
    console.log(sort(persons, 'name', false))

}

// array map

{
    let arr = ["20", null, undefined, "5", {}, 700];

    let arrMap = arr.map(function (item, index, arr) {
        let number = parseInt(item);
        return isNaN(number) ? item : number;
    });

    console.log(arrMap);
}

// array reduce

{
    let arr = ["0", 5, 3, "string", null];

    let result = arr.reduce(function (previousValue, currentValue) {
        if (typeof (currentValue) === 'number') {
            previousValue *= currentValue;
        };
        return previousValue
    }, 1)
    alert(result);
}

// object filter
{
    let phone = {
        brand: "meizu",
        model: "m2",
        ram: 2,
        color: "black",
    };

    function filter(object, func) {
        for (let key in object) {
            if (!func(key, object[key])) {
                delete object[key];
            }
        }
        return object;
    }

    console.log(filter(phone, (key, value) => key == "color" || value == 2));
}

// object map
{
    function map(object, func) {
        const fields = [];
        for (let key in object) {
            fields.push(func(key, object[key]));
        }
        return Object.assign({}, ...fields);
    }
    console.log(
        map({ name: "Иван", age: 17 }, function (key, value) {
            let result = {};
            result[key + "_"] = value + "$";
            return result;
        })
    );
}

//sum recursion
{
    function sum(n) {
        if (n === 0) {
            return 0
        } else {
            return n + sum(n - 1);
        }
    }

    console.log(sum(100));

}