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

// function sort (arr, key, boolean = true) {
//     if(boolean) {
//         arr.sort(((a, b) => a[key] > b[key] ? 1 : -1));
//     }
//     else{
//         arr.sort(((a, b) => a[key] < b[key] ? 1 : -1));
//     }
// }

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

//2.array map
// let arr = ["1", {}, null, undefined, "500", 700];
// console.log(arr.map(item => isNaN(item) ? item : parseInt(item) || null));

//3.array reduce
// let arr = ["0", 5, 3, "string", null];
// let reduce = arr.reduce (function(a, b) {
//     return((typeof(b) === 'number') ? (a *= b) : a);
// }, 1)
// console.log(reduce);

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

// function filter (obj, f){
//     for(let key in obj){
//         if(!f(key, obj[key])){
//             delete obj[key];
//         }
//     }
//     return obj;
// }

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

//5.object map
// function map (obj, f){
//     let res = {};
//     for(let key in obj){
//         res[key] = obj[key];
//     }
//     return res;
// }

// let objMap = map({name: "Иван", age: 17},function(key,value){
//     let result = {};
//     result[key+"_"] = value + "$";
//     return result;
// }); 

// console.log(objMap);

//6.Рекурсия
//Sum
// function sum (num){
//     if(num === 0){
//         return 0;
//     }
//     else{
//         return num + sum(num-1);
//     }
// }

// console.log(sum(3));

//HTML Tree
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 a (obj){
    let res = '';
    for(let elem in obj){
        let el = obj[elem];
        if('subTags' in el){
            res += `<${el['tagName']}> ${a(el['subTags'])} </${el['tagName']}>`;
        }
        else{
            res += `<${el['tagName']}> ${el['text']} </${el['tagName']}>`;
        }
    }
    return res;
}

let str = '';
if('tagName' in someTree){
    str += `<${someTree.tagName} border='1'>`;
}
str += a(someTree['subTags']);
str += `</${someTree.tagName}>`;

console.log(str);
document.write(str);