Can Anyone help me write this in java script

Exercise One: In this exercise you will be creating two functions.

// Function One: Will be called 'multiply'.
// This function will take two parameters, both numbers
// This function will return the two numbers multiplied together.

// Function Two: Will be called 'calculator'.
// This function will take three parameters,
// First will be a callback function,
// Second and Third will be numbers.
// This function will return the two numbers passed into the callback function.

// NOTE: You can use the multiply function to test the calculator function, but understand that
// other callback functions will be passed into it as a test.

You are viewing a single comment. View All
Answered by mwilki7 (277) [earned 5 cycles]
mwilki7 (277)

What part of these functions do you need help writing?

mwilki7 (277)

@faziz6006
Looks like the multiply function is missing:

function multiply (p1, p2)
{
return p1 * p2;
}
mwilki7 (277)

@faziz6006
Function 2 will look something like:

function secondFunc(First, Second, Third)
{
....
}

"First" is going to be the name of a function passed as a parameter to "secondFunction()"
Next you just need a return statement before you call "First(...)" with Second and Third as parameters being passed to First

faziz6006 (2)

@mwilki7 im lost lol

function secondFunc( wait where did u get the second function from?

faziz6006 (2)

function calculater ( call back function, 1, 2) ?

faziz6006 (2)

@mwilki7 im so overwhelmed plez help

mwilki7 (277)

@faziz6006
secondFunc() is just the name I chose to call Function 2, now I see it's supposed to be called "calculator"

Here it is with more helpful variable names:

function calculator (callback, a, b)
{
return *use callback here and pass a and b*
}
faziz6006 (2)

@mwilki7 ok so ..

function calculator (callback (I thought this was a function, or is this funcation called a callback?), 1, 3);

mwilki7 (277)

@faziz6006
calculator is a function
callback is also a function

you can pass functions as parameters and call them later like so:

function calculator(callback, a, b)
{
callback(a,b);
}
faziz6006 (2)

@mwilki7 oh, so so then i just return the two numbers passed into the callback?

so my answer is :

function calculator (callback, 1, 2)
{
callback(1, 2)
}
return (1, 2) ;

faziz6006 (2)

@mwilki7 I think i need to go back and study javacript from the beginning again. its just that this course work is due and i dont get why they gave me work before i even started learning java script,

mwilki7 (277)
function calculator (callback, 1, 2)

JavaScript doesn't allow numbers to be names of variables

function calculator (callback, p1, p2)

or whatever name you think fits the 2nd and 3rd parameters.

You almost had it, just put the return statement inside the brackets like so:

return callback(p1, p2)
}

The return statement

faziz6006 (2)

@mwilki7 oh, but didint it say the function will take three parameters and two will be numbers? but ok i will call it p1 and p2

so here is the final answer?

function calculator (callback, p1, p2)
{
callback (p1, p2)
return callback(p1, p2)
}

mwilki7 (277)

@faziz6006
Somewhere in the program someone will call
calculate(...)

They are responsible for putting numbers in the parameters:

*some code before*
calculate(multiply, 1, 2)
*some code after*

When we create the function, we use variables for parameters.
For this case, p1 and p2 must be treated like numbers otherwise program may throw errors.

function calculate(callback, p1, p2)
{
...
}

calculate(multiply, 1, 6);
// callback will get multiply
// p1 will get 1
// p2 will get 6
faziz6006 (2)

you lost me, im sorry , any chance u can give the answer so i can submit it, then im going start the video from the beginning and try to figure out where i got lost.

mwilki7 (277)

@faziz6006 Your answer above my previous post should work.

faziz6006 (2)

@mwilki7 there is a error saying :
ReferenceError: multiply is not defined
"Should return the two numbers passed into the callback function"

mwilki7 (277)

@faziz6006
Did you insert this one?

function calculator (callback, p1, p2)
{
callback (p1, p2)
return callback(p1, p2)
}
faziz6006 (2)

@mwilki7 yes maybe im not writing it in the right space: here is the full question , where should i write my answer? and what does the last sentence ask me to do ?

// Exercise One: In this exercise you will be creating two functions.

// Function One: Will be called 'multiply'.
// This function will take two parameters, both numbers
// This function will return the two numbers multiplied together.

// Function Two: Will be called 'calculator'.
// This function will take three parameters,
// First will be a callback function,
// Second and Third will be numbers.
// This function will return the two numbers passed into the callback function.

// NOTE: You can use the multiply function to test the calculator function, but understand that
// other callback functions will be passed into it as a test.

function calculator (callback, p1, p2)
{
callback (p1, p2);
return callback(p1, p2);
}

//Call MULTIPLY to multiply the two numbers
var product=MULTIPLY(number1, number2);

faziz6006 (2)

@mwilki7 gosh, thanks for bearing with me, any tools u suggest so i can start java scrpt from beinging , im like brand new to this

mwilki7 (277)

@faziz6006 JavaScript programming tutorials on youtube may be a good start.
If you want some place to practice it, try opening nodejs projects on repl (that way you don't need to know HTML).

Try coming up with little programs that sound fun to make, and if there's anything you don't know google it.
Most of my own code is just snippets of what I found online.

JustARatherRidi (190)

@faziz6006 Hey, I think you might need a refresher on how functions actually work. Give this shamelessly advertised answer of mine a read and let me know whether it helps.