/* ЗАДАНИЕ makeProfileTimer */
function makeProfileTimer() {
    let start = performance.now();
    return () => performance.now() - start;
}

let timer = makeProfileTimer();
alert('Замеряем время работы этого alert');
alert(timer());


/* ЗАДАНИЕ makeSaver */
function makeSaver(func){
    let saver = func();
    return () => saver;
}

let saver = makeSaver(Math.random);
let value1 = saver()
let value2 = saver()
console.log(value1 === value2);
let saver2 = makeSaver(() => console.log('saved function called') || [null, undefined, false, '', 0, Math.random()][Math.ceil(Math.random()*6)])
let value3 = saver2()
let value4 = saver2()
console.log(value3 === value4);


/* ЗАДАНИЕ Final Countdown */
((n=5) => {
    setTimeout(function run() {
    console.log(n--);
    let stop = setTimeout(run, 1000);
    if(n===0) {clearTimeout(stop); console.log("поехали!")}
    }, 1000)
})();


/* ЗАДАНИЕ myBind */
function myBind(func, context, args){
    return (...undArgs) => {
        let counter = 0;
        args.forEach((item, index) => {
            (item === undefined) && args.splice(index, 1, undArgs[counter++]);
        })
        return func.apply(context, args);
    }
}

let pow5 = myBind(Math.pow, Math, [undefined, 5]);
let cube = myBind(Math.pow, Math, [undefined, 3])
console.log(pow5(2));
console.log(cube(3));
let chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9]);
console.log(chessMin(-1,-5,3,15));
let zeroPrompt = myBind(prompt, window, [undefined, "0"]);
let someNumber = zeroPrompt("Введите число");
console.log(someNumber);
console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d'));

// Переделанная таблицы умножения
function multiplicationTable() {
    let table = document.createElement('table');
    table.style.fontFamily = 'sans-serif';
    let arrNum = [];
    for (let i = 1; i <= 10; i++) {
        arrNum[i] = [];
        for (let j = 1; j <= 10; j++) {
            arrNum[i][j-1] = i * j;
        }
    }
    for (let row = 1; row <= arrNum.length-1; row++) {
        let tr = document.createElement('tr');
        for (let col = 0; col < arrNum.length-1; col++) {
            let td = document.createElement('td');
            td.innerText = arrNum[row][col];
            td.onmouseout = function(event){
                this.style.backgroundColor = '';
                Array.from(this.parentElement.children).forEach(item => item.style.backgroundColor = '');
                colTable(this.cellIndex, '');
            }
            td.onmouseover = function(event){
                Array.from(this.parentElement.children).forEach(item => item.style.backgroundColor = '#aaa');
                colTable(this.cellIndex, '#aaa');
                this.style.backgroundColor = '#6d75cb';
            }
            tr.append(td);
        }
        for (let cell of tr.cells) {
            cell.style.border = '2px solid #000';
            cell.style.padding = '10px';
            cell.style.textAlign = 'center';
            cell.classList.add('td');
        }
        table.append(tr);
    }
    document.body.append(table);
    function colTable(index, str){
        for (let row of document.querySelector('table').rows) {
            row.cells[index].style.backgroundColor = str;
        }
    }
} // Таблица умножения
multiplicationTable();