Functions - Exercise

by Colin Cheevers

JavaScript

/*
 * Functions - Exercise
 * 
 * 1. Create a closure that returns a function that accepts msg and limit. The returned
 *    function will alert the msg, but only up to the limit times
 * 2. Using the provided convert() function below, use .bind() to create new functions that
 *    can convert various units, like celcius to fahrenheit, miles to kilometers, etc
 * 3. Use an IIFE make the provided function output the numbers in the order expected
 */

function createWarn(msg, limit) {
    
    var a = 0;
   // var b = msg
    
    return function(){
        if(a <= limit)
        {
        	console.log(msg, a);
            a++;
        }    
         
    };
}    

var warn = createWarn("You have been warned : ", 3);

// 1. closure here
warn(); // 'You have been warned 1 time'
warn(); // 'You have been warned 2 times'
warn(); // 'You have been warned 3 times'
warn(); // noop


// 2. bind here
function convert(factor, offset, input) {

    return (input + (offset || 0)) * factor;

}

var fn = convert.bind(null,);



// 3. IIFE here
for (var i = 1; i <= 5; i++) {
	setTimeout(function() {
		console.log(i);
	}, i * 1000);
}
*/