Question

Please help as soon as you can Thnx! // ==== Callbacks ==== /* Step 1: Create...

Please help as soon as you can Thnx!

// ==== Callbacks ====

/* Step 1: Create a higher-order function

* Create a higher-order function named consume with 3 parameters: a, b and cb

* The first two parameters can take any argument (we can pass any value as argument)

* The last parameter accepts a callback

* The consume function should return the invocation of cb, passing a and b into cb as arguments

*/

/* Step 2: Create several functions to callback with consume();

* Create a function named add that returns the sum of two numbers

* Create a function named multiply that returns the product of two numbers

* Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!"

*/

/* Step 3: Check your work by un-commenting the following calls to consume(): */

// console.log(consume(2, 2, add)); // 4

// console.log(consume(10, 16, multiply)); // 160

// console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you!

// ==== Closures ====

// Explain in your own words why nestedfunction can access the variable internal.

// Explanation:

const external = "I'm outside the function";

function myFunction() {

console.log(external);

const internal = "Hello! I'm inside myFunction!";

function nestedFunction() {

console.log(internal);

};

nestedFunction();

}

myFunction();

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

// ==== Callbacks ====

/* Step 1: Create a higher-order function

* Create a higher-order function named consume with 3 parameters: a, b and cb

* The first two parameters can take any argument (we can pass any value as argument)

* The last parameter accepts a callback

* The consume function should return the invocation of cb, passing a and b into cb as arguments

*/

function consume(a, b, cb) {

    return cb(a, b);

}

/* Step 2: Create several functions to callback with consume();

* Create a function named add that returns the sum of two numbers

* Create a function named multiply that returns the product of two numbers

* Create a function named greeting that accepts a first and last name and returns "Hello first-name last-name, nice to meet you!"

*/

function add(a, b) {

    return a + b;

}

function multiply(a, b) {

    return a * b;

}

function greeting(fname, lname) {

    return "Hello " + fname + " " + lname + ", nice to meet you!";

}

/* Step 3: Check your work by un-commenting the following calls to consume(): */

console.log(consume(2, 2, add)); // 4

console.log(consume(10, 16, multiply)); // 160

console.log(consume("Mary", "Poppins", greeting)); // Hello Mary Poppins, nice to meet you!

// ==== Closures ====

// Explain in your own words why nestedfunction can access the variable internal.

// Explanation:

const external = "I'm outside the function";

function myFunction() {

    console.log(external);

    const internal = "Hello! I'm inside myFunction!";

    function nestedFunction() {

        console.log(internal);

    };

    nestedFunction();

}

myFunction();

Add a comment
Know the answer?
Add Answer to:
Please help as soon as you can Thnx! // ==== Callbacks ==== /* Step 1: Create...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you....

    Help with programming in C++ Phase 1 is complete, please help with phase 2. Thank you. Phase 1 You must design 3 algorithms, and provide both a flow chart and pseudo code for the algorithms.    Algorithm descriptions: Given an integer parameter named current_number and two constant global variables: const int MIN_NUMBER = 1; const int MAX_NUMBER = 8; Create an algorithm named forward, that will advance ONE value through a sequence of numbers 1, 2, 3 ... MAX_NUMBER. In...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   and please put comment with code! Problem:2 1. Class Student Create a "Hello C++! I love CS52" Program 10 points Create a program that simply outputs the text Hello C++!I love CS52" when you run it. This can be done by using cout object in the main function. 2. Create a Class and an Object In the same file as...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • WEB230 - JavaScript 1 Assignment 6a Use Events and DOM manipulation to create a playable Tic...

    WEB230 - JavaScript 1 Assignment 6a Use Events and DOM manipulation to create a playable Tic Tac Toe game. Do the following using JavaScript. Do not modify the HTML file. When you see this symbol: save and refresh the browser (this should happen automatically if you are using Brackets with Live Preview), then check your code to make sure it is working before you proceed. 1. Create two variables called "playerX" and "playero". Set them to the DOM element with...

  • Can you please write the two C++ modules below is a step by step instuctions to...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

  • Can you please write the two C++ modules below is a step by step instuctions to follow that will ...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

  • The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and incl...

    The both files are included. Where are these which are colorful. Point.h and point.cpp Hor this assignment you are provided the files for a class called point. Download the h and .cpp files and include them in your project. IheじML diagram below details the class Point くくfriend>> ostream& operator.((ostream&, point&) <ごfriend::. İstream& operator:..イ1stream&-point& - : double - v doublc getX) double getYO double - sctX( double): void - set Y(double) : void - point(double-0.0, double-0.0 operator-(const point& bool perator< const...

  • C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...

    C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....

  • 10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated...

    10. Write a one-page summary of the attached paper? INTRODUCTION Many problems can develop in activated sludge operation that adversely affect effluent quality with origins in the engineering, hydraulic and microbiological components of the process. The real "heart" of the activated sludge system is the development and maintenance of a mixed microbial culture (activated sludge) that treats wastewater and which can be managed. One definition of a wastewater treatment plant operator is a "bug farmer", one who controls the aeration...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT