/* function makeProfileTimer() {
    let time = performance.now();
    console.log(`${time} in milisec`);
    return time;
//     console.time('test of speed');
//    let time = performance.now();
//    console.log(`${time} in milisec`);
//    console.timeEnd('test of speed');
//    return time; 
}
let timer = makeProfileTimer();
alert('Замеряем время работы этого alert');
let timer2 = makeProfileTimer();
alert(timer2 - timer);
 */

 
function makeSaver(x){
        let called = true;
        let number;
    return function a (){
        if(called){
            number = x();
            called = false;
        } 
        return number;
  } 
} 

let saver = makeSaver(Math.random);

let value1 = saver();
let value2 = saver();

console.log(value1);
console.log(value2);

console.log(value1 === value2);

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

console.log(value3 === value4);

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


/* 
(function(seconds) {
    let counter = seconds;

    setTimeout(function countDown(){
        if(counter >= 1 ){
        console.log(`${counter} // pause 1 sec`);
        setTimeout(countDown,1000);
    }
        if (counter <= 0) {
            console.log("GOOOOOOOOOOOOOOOOOO DRINK BEER BRATAN");
            clearTimeout(counter);
        }
        counter--;
    },1000);
})(5); */

/* function myBind (fn,context, bindedparams){
    console.log(bindedparams);
    return function(...params){
        let combinedparams = [...bindedparams];
       
        for(let i =0; i < params.length;i++){
            combinedparams.splice(combinedparams.indexOf(undefined),1,params[i]);
        }
        console.log(combinedparams);
        return fn.apply(context, combinedparams);
    }
}
 // works
let pow5 = myBind(Math.pow,Math,[undefined,5]);
console.log(pow5(2));
var cube = myBind(Math.pow, Math, [undefined, 3]);
console.log(cube(3));
 //works
var chessMin = myBind(Math.min, Math, [undefined, 4, undefined, 5,undefined, 8,undefined, 9])
console.log(chessMin(-1,-5,3,15));
console.log(myBind((...params) => params.join(''), null, [undefined, 'b', undefined, undefined, 'e', 'f'])('a','c','d') === 'abcdef') */