
////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)


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





function filter(obj, funk) {
    let rest = {}
    for (let key in obj){
        if (funk(key, obj[key])){
            rest[key] = obj[key]
        }
    }
    return rest
}
console.log(filter(phone,(key,value) => key == "color" || value == 2));


///Array map

function map(obj, funk) {
    
    let newObj = {}
    for (let key in obj){
        let temp = funk(key, obj[key]);
        
        for(let _key in temp) {
            newObj[_key] = temp[_key];
        }
    
    }
    return newObj

}


console.log(map({name: "Иван", age: 17},function(key,value){
    var result = {};
    result[key+"_"] = value + "$";
    return result;
}))

////Sum
//Не поняла точно задания, потому написала два варианта

function sumRecurs(a, b, c){

    a += b;
    if(a < c) {
       return sumRecurs(a, b, c)
    }
    return a

}
console.log(sumRecurs(2, 4, 16))


function sumRecurs2 (a, b, c) {

    if(c > 0) {
        a += b;
        c = c - 1
       return sumRecurs2(a, b, c)
    }
    return a

}

console.log(sumRecurs2(2, 4, 5)) 


//toHtml
///уже вроде делала
var someTree = {
    tagName: "table", //html tag
    subTags: [ //вложенные тэги
        {
            tagName: "tr",
            subTags: [
                {
                tagName: "td",
                text: "some text",
                },
                {
                tagName: "td",
                text: "some text 2",
                }
            ]
        }
    ],
    attrs: 
    {
        border: 1,
    },
}


function toHtml(obj) {
    debugger

    var element = document.createElement(obj.tagName);
    
    if (obj.text) {
        element.innerHTML = obj.text          
    }

    if (obj.attrs) {
        for (let key in obj.attrs)  {
            element[key] = obj.attrs[key]
        }
    }
        
    if (obj.subTags) {
        for (let i = 0; i < obj.subTags.length; i++) {
            element.appendChild(toHtml(obj.subTags[i]));
        }
    }

    return element;
}
 //document.body.appendChild(toHtml(someTree));