
////Sort


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



function sort(arr, key, boolean = true) {
    let middleArr = [];

    for (let i = 0; i < arr.length; i++) {
        middleArr.push(arr[i]);
    }

    for (let j = 0; j < middleArr.length; j++) {
        let index = 0

        for (let k = 1; k < middleArr.length;) {

            if (middleArr[index][key] < middleArr[k][key]) {
                [middleArr[index], middleArr[k]] = [middleArr[k], middleArr[index]]
                k += 2
                index += 1

            } else {
                k += 1
                index += 1
            }

        }

    }
    if (boolean == true) {
        middleArr.reverse()
    }

    return middleArr
}

console.log(sort(persons, "age"));
console.log(sort(persons, "name", false))



///////Array map
let array = ["1", {}, null, undefined, "500", 700]
let newArray = array.map(function (num) {
    if (!num === false && !isNaN(+num)) {
        return +num
    }

    return num

})
//console.log(newArray)


//Array reduce
let reduceArray = ["0", 5, 3, "string", null]
let result = reduceArray.reduce(function(a, b){
    if(typeof a !== "number"){
        a = b
        return a
    } else if 
     (typeof b !== "number"){
        return a
    } else {
        a = a*b
    }
    return a
})
console.log(result)