In: Computer Science
// ==== Challenge 1: Write your own closure ====
// Write a closure of your own creation.
// Keep it simple! Remember a closure is just a function
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
// ==== Challenge 2: Implement a "counter maker" function ====
const counterMaker = () => {
// IMPLEMENTATION OF counterMaker:
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
// 2- Declare a function `counter`. It should increment and return `count`.
// NOTE: This `counter` function, being nested inside `counterMaker`,
// "closes over" the `count` variable. It can "see" it in the parent scope!
// 3- Return the `counter` function.
};
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
// ==== Challenge 3: Make `counterMaker` more sophisticated ====
// It should have a `limit` parameter. Any counters we make with `counterMaker`
// will refuse to go over the limit, and start back at 1.
// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
};
If you have any doubt with the programs feel free to comment
Challenge 1
function bmiCal(weight, height) {
bmi = weight / (height * height);//calculating bmi
bmi = bmi.toFixed(2);//limiting to decimal place
return function () {//using the power of closure to use the variable bmi
//checking bmi and health status
if (bmi <= 18.5)
return "BMI: " + bmi + " Index: " +"Thin";
else if (bmi >= 18.6 && bmi <= 24.9)
return "BMI: " + bmi + " Index: " +"Healthy";
else if (bmi >= 25 && bmi <= 29.9)
return "BMI: " + bmi + " Index: " +"Overweight";
else
return "BMI: " + bmi + " Index: " +"Obese";
};
}
//weight in kg and height in meters
console.log(bmiCal(60, 1.66)());//calling bmiCal() and retrun anonymous function
Challenge 2 & 3
//challenge 2 & 3
const counterMaker = (limit) => {
let count =0;
function counter(){
count++;//increasing count
if(count > limit){//checking limit
count = 1;
return count;
}
else{
return count;
}
}
return counter;
}
const myCounter = counterMaker(5);//limit is 5
console.log(myCounter());// 1
console.log(myCounter());// 2
console.log(myCounter());// 3
console.log(myCounter());// 4
console.log(myCounter());// 5
console.log(myCounter());// 1
Challenge 4
//challenge 4
const counterFactory = () => {
let count =0;
return {
increment : function(){//creating increament object
return ++count;//increasing count
},
decrement: function(){//creating decreament object
return --count;//decreasing object
}
}
}
//showing output
const myCounter = counterFactory();
console.log(myCounter.increment());
console.log(myCounter.decrement());