Question

In: Computer Science

// ==== Challenge 1: Write your own closure ==== // Write a closure of your own...

// ==== 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.

};

Solutions

Expert Solution

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());


Related Solutions

Make in c++ this Programming Challenge 1. Design your own linked list class to hold a...
Make in c++ this Programming Challenge 1. Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don’t forget to add a destructor that destroys the list. Demonstrate the class with a driver program. 2. List Print Modify the linked list class you created in Programming Challenge 1 to add a print member function. The function should display all the values in the linked list. Test...
The definitions of racism might not be your own perceptions. What are your own perceptions of race and how will you prepare yourself to challenge your beliefs moving forward?
  The definitions of racism might not be your own perceptions. What are your own perceptions of race and how will you prepare yourself to challenge your beliefs moving forward? Racist perceptions of black people spawn from racist policy and ideas and not black behavior. You will need to begin to recognize these ideas in order to be an antiracist. Can you name three perceptions of black people which stem from racist ideology and not black behavior? How does viewing...
Write your own autoethnography on your own experience within the African American culture . Must be...
Write your own autoethnography on your own experience within the African American culture . Must be health-based and integrated with facts.
Write your own autoethnography on your own experience within a culture. Must be health-based and integrated...
Write your own autoethnography on your own experience within a culture. Must be health-based and integrated with facts (e.g. experiencing smoking, air pollution in crowded city, unsafe road conditions in a foreign country, etc). Must be one page long, single-spaced.
Write your own autoethnography on your own experience within a culture. Must be health-based and integrated...
Write your own autoethnography on your own experience within a culture. Must be health-based and integrated with facts (e.g. experiencing smoking, air pollution in crowded city, unsafe road conditions in a foreign country, etc)
Solve the below questions using your own words PLEASE!! Make sure to write by your own...
Solve the below questions using your own words PLEASE!! Make sure to write by your own words or paraphrase 1. What is the difference between Windows and Linux server 2. Give some advantages and disadvantages Windows and Linux Operating System
In your own word please explain and write in complete sentences. 1. What is diabetes and...
In your own word please explain and write in complete sentences. 1. What is diabetes and what causes it? 2. List and explain the different types of diabetes. 3. How do you care for diabetes? 4. Explain what causes a person to faint?
1, Write a definition of respiratory failure in your own words 2, What is the difference...
1, Write a definition of respiratory failure in your own words 2, What is the difference between Type I respiratory failure and Type II respiratory failure? 3, What is pursed-lip breathing? 4,How does pursed-lip breathing improve ventilation in a patient with COPD? 5,List the key elements included in the 'GOLD' and Lung Foundation Australia guidelines for a long-term COPD management plan.
1 Write a few sentences addressing each of these prompts. In your own words, what is...
1 Write a few sentences addressing each of these prompts. In your own words, what is the difference between electric force, electric field, and electric charge? You may find it easier to describe how the three concepts are related. If two charges are observed to repel one another, what can you say about the sign of each? What if the two attract one another? With only this information (whether they attract or repel) can you identify which charge is which...
1. Write at least two sentences in your own words to explain the difference of using...
1. Write at least two sentences in your own words to explain the difference of using flexible boundary channel and the rigid boundary channel (based on the design considerations presented here). 2. Answer True or False and justify your answer:     a. The rigid boundary channel is recommended for channels with very high velocities because it can easily resist the shear stress caused by the water flow.     b. The trapezoidal cross section (with a value of m = 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT