// makeProfileTimer ==============================================================================


makeProfileTimer = () => {
    const starTime = performance.now()
    console.log('START: ' + starTime + ' Ms')

    return () => {
        const finishTime = performance.now()
        console.log('FINISH: ' + finishTime + ' Ms')
        const result = finishTime - starTime

        return console.log('RESULT: ' + result + ' Ms')
}
}

const timer = makeProfileTimer()
console.log('Замеряем время работы этого console.log');
console.log(timer())


// makeSaver ==============================================================================


makeSaver = (arg) => {
    let result
    let stopFn = true
    return () => {
        if(stopFn){
            stopFn = false
            result = arg()
        }
        console.log(result)
        return result
    }
}


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)


let namePrompt = prompt.bind(window, 'Как тебя зовут?')
let nameSaver = makeSaver(namePrompt)
alert(`Привет! Prompt еще не было!`)
alert(`Привет ${nameSaver()}. Только что запустился prompt, первый и последний раз`)
alert(`Слушай, ${nameSaver()}, го пить пиво. Ведь prompt был только один раз`)


// Final Countdown ==============================================================================


(finalCountdown = (counterVal) => {
    countingFn = () => {
    counterVal ? console.log(counterVal--) : clearInterval(interval)   
    }
    const interval = setInterval(countingFn,1000)
})(5);


// &&


const finalCountdown = (counterVal) => {
    let result
     return (() => {

        for(let i = counterVal; i >= 0; i--){
        result=counterVal
        if(result == 0) return   
        }

        console.log(result--)
        setTimeout(finalCountdown,1000,result)
        
    })()
}

finalCountdown(5)


// myBind ==============================================================================


function myBind( fn, isThis, arr ) {
    let result

    return function (...passParam) {
        for(let i = 0; i < passParam.length; i ++){

            for(let j = 0; j < arr.length; j ++){
                
                if(arr[j] === undefined){
                    arr[j] = passParam[i]
                    passParam.shift()
                }
            }
        }
        result = fn.call(isThis,...arr)
        return console.log(result)
    }
}


var pow5 = myBind(Math.pow, Math, [undefined, 5])
pow5(2)                

var cube = myBind(Math.pow, Math, [undefined, 3]) 
cube(3)

var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
chessMin(-1,-5,3,15)

var zeroPrompt = myBind(prompt, window, [undefined, "0"])
var someNumber = zeroPrompt("Введите число")

myBind( (...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'] ) ('a','c','d') === 'abcdef'


// ============================================================================================================================================================






