//Сама таблица Пифагора, такая же как и в домашке раньше
let multiplyTable = []
for (let i = 0; i <=10; i++) {
    multiplyTable[i] = []
    for (let j = 0; j <=10; j++) {
        multiplyTable[i][j] = i * j
    }
}

let tableContainer = document.querySelector('.multipleTable')

let table = document.createElement('table')
table.border = "1"
for (let i = 0; i < multiplyTable.length; i++) {
    let row = document.createElement('tr')

    if (i == 0) {
        let cell = document.createElement ('td')
        cell.innerHTML = '&#9959;'
        row.appendChild(cell)
    } else {
        let cell = document.createElement('td')
        cell.innerText = i
        row.appendChild(cell)
    }

    for (let j = 1; j < multiplyTable[i].length; j++) {
        if (i == 0) {
            let cell = document.createElement('td')
            cell.innerText = multiplyTable[1][j]
            row.appendChild(cell)
        } else {
            let cell = document.createElement('td')
            cell.innerText = multiplyTable[i][j]
            row.appendChild(cell)
        }
    }
    
    table.appendChild(row)
}

tableContainer.appendChild(table)

//подсветка строки и столбца с замыканиями
let rows = document.querySelectorAll('tr');

for (let tr of rows) {
    let tdCounter = 0;
    for (let td of tr.children) {
        let tdCounterToRemember = tdCounter;
        td.onmouseover = function () {
            for (let column of rows) {
                column.children[tdCounterToRemember].style.backgroundColor = '#bcf'
                column.children[tdCounterToRemember].style.color = '#fff'
            }

            for (let rowCell of tr.children) {
                rowCell.style.backgroundColor = '#bcf'
                rowCell.style.color = '#fff'
            }
            td.style.backgroundColor = '#6dd'
            td.style.color = '#fff'
        }
        td.onmouseout = function () {
            for (let column of rows) {
                column.children[tdCounterToRemember].style.backgroundColor = ''
                column.children[tdCounterToRemember].style.color = ''
            }

            for (let rowCell of tr.children) {
                rowCell.style.backgroundColor = ''
                rowCell.style.color = ''
            }
            td.style.backgroundColor = ''
            td.style.color = ''
        }
        tdCounter++
    }
}

//makeProfileTimer
function makeProfileTimer () {
    let startTime = performance.now()
    function innerTimer () {
        let endTime = performance.now()
        let workTime = endTime - startTime;
        return workTime;
    }
    return innerTimer;
}


//makeSaver
function makeSaver (func) {
    let result;
    function returnResult () {
        if (result === undefined) {
            result = func()
        }
        return result;
    }
    return returnResult;
}

//Final Countdown
function countdown (numb) {
    let counter = numb;
    let countdown = setTimeout(function showCountdown () {
        if (counter < 1) {
            console.log('Поехали!')
            return;
        }
        console.log(counter)
        counter--
        countdown = setTimeout(showCountdown, 1000)
        }, 1000)
}

//or
// let n = 5
// for (let i = n, j = 1; i >= 0; i--, j++) {
//     if (i === 0) {
//         setTimeout(console.log, 1000 * j, 'Поехали!')
//         break
//     }
//     setTimeout(console.log, 1000 * j, i)
// }

//MyBind
    function myBind(functionToBind, context, args) {
        function boundFunction (...newArgs) {
            let commonArgs = []
            let functionResult
            let counter = 0
            for (let argument of args) {
                if (argument === undefined) {
                    commonArgs.push(newArgs[counter])
                    counter++
                } else {
                    commonArgs.push(argument)
                }
            }
            if (context) {
                functionResult = functionToBind.call(context, ...commonArgs)
            } else {
                functionResult = functionToBind(...commonArgs)
            }
            return functionResult
        }
        return boundFunction
    }

