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() {
    
    var limit = 1;
    var msg = "You have been warned" + limit + " times";
    
    return function(msg, limit){
		limit = limit +1;
    };
}    

var warn = createWarn();

// 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;

}



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