//то, что мы на занятии делали
fetch('https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.json').then(res => res.json())
.then(data => {
    for (let key of Object.keys(data)) {
        let option = document.createElement('option')
        option.value = key;
        option.innerText = key;
        countrySelect.append(option)
    }

    countrySelect.onchange = () => {
        citySelect.innerHTML = ''
        for (let city of data[countrySelect.value]) {
            let option = document.createElement('option')
            option.value = city;
            option.innerText = city;
            citySelect.append(option)
        }
    }
})

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

function sort (arr, key, isIncrease) {
    if (isIncrease != undefined && isIncrease == false) {
        arr.sort((obj, obj2) => obj[key] < obj2[key] ? 1 : -1)
        return arr;
    }

    arr.sort((obj, obj2) =>  obj[key] > obj2[key] ? 1 : -1)
    return arr;
}

sort(persons, "age");
sort(persons, "name", false);

//array map
["1", {}, null, undefined, "500", 700].map(a => {
    if (typeof a === "string") {
        a = +a
    }
    return a;
});

//array reduce
["0", 5, 3, "string", null].reduce((a, b) => (typeof b === "number") ? a * b : a, 1);

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

function filter(obj, f) {   
    for (let [key, value] of Object.entries(obj)) {
        if (f(key, value) === false) {
            delete obj[key]
        }
    }
    return obj;
};

filter(phone,(key,value) => key == "color" || value == 2);

//object map
function map(obj, f) {
    let newObj = {}
    for (let [key, value] of Object.entries(obj)) {
        newObj = {...newObj, ...f(key, value)}
    }
    return newObj
};

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

//Sum
//вариант, где нужно указать, до какого числа считать и шаг
function progressionSum(lastNumber, step = 1) {
    if (lastNumber <= 1) {
        return 1;
    }

    let sum = number + progressionSum(lastNumber - step, step)
    return sum;
}
//или вариант, где нужно указать, сколько элементов нам нужно сложить, с какого числа начинаем и шаг.. я не шарю в математике я не знаю как правильно простите спасибо
function progressionSum(summandsNumber, firstNumber = 1, step = 1) {
    if (summandsNumber < 1) {
        return 0;
    }

    let sum = firstNumber + progressionSum(summandsNumber - 1, firstNumber + step, step)
    return sum;
}

//HTML Tree
var someTree = {
    tagName: "table", //html tag
    children: [ //вложенные тэги
        {
                    tagName: "tr",
                    children: [
                        {
                            tagName: "td",
                            text: "some text",
                        },
                        {
                            tagName: "td",
                            text: "some text 2",
                        }
                    ]
        }
    ],
    attrs: 
    {
        border: 1,
    },
}

function treeConstructor(object) {
    let table = `<${object.tagName}`
    if("attrs" in object) {
        for (let [key, value] of Object.entries(object.attrs)) {
            table +=  ` ${key}="${value}"`
        }
    }
    table += `>`
    if ("text" in object) {
        table += object.text
    }
    document.write(table)

    if ("children" in object) {
        for (let child of object.children) {
            treeConstructor(child)
        }
    }  

    table = ''
    table += `</${object.tagName}>`
    document.write(table)  
}

treeConstructor(someTree)