Stuck on functions :\

Hai, I am stuck on functions, never had enough education on algebra. Can anybody help me out?

1 Like

Hi @josh

Take a look at this:

2 Likes

Thanks for helping out Billy.

2 Likes

Hi @bisk, due to having to move my mum to a home for the elderly, I have not been able to thank you, so, Thank you! as of today I hope with your link I can get more mass on functions, watched the video several times now and I think I can give it another go…

Hi @josh.
Happy to help. I know the feeling as I haven’t used any of this kind of math for over 30 years. Lucky for us that their are some great free learning resources out there like Youtube, Khan Academy and the like. I hope everything works out well for your mum by the way.
Cheers
Bill

Unfortunattely, just don’t get my head around this … FUnCtions :frowning:

Don’t stress. If you haven’t had much to do with algebra then you might find it useful to do an intro course in that first. Knowing some basic algebra may help you get your head around functions. I am no expert but here’s my suggestions.

It is Khan Academy again so if you like their style of teaching this may be good:
https://www.khanacademy.org/math/algebra-home/alg-intro-to-algebra
Another youtube video about algebra that looks popular is this:


Sorry I can’t be more help but like I said I’m no expert in this stuff either plus I’m a terrible teacher.

Good luck.

I am stuck on the concept of this example. I have seen it partially explained in other materials and I get the GENERAL idea of what is happening, but I don’t get WHY or HOW it happens. Here is the code:

function multiple(x) {

    function fn(y)
    {
        return x * y;
    }
    
    return fn;
}

var triple = multiple(3);
triple(2); // returns 6
triple(3); // returns 9


I get that the binding “triple” ends up storing the function “fn(y)”. And I read somewhere that when a binding is created that stores a function, if a value had been passed into the function at the time of the binding, then that value is saved as well somehow in the binding.

Thus when the statement “var triple = multiple(3);” is done, that 3 is somehow trapped or stored in the function variable such that when the variable is called later as a function, that 3 is going to be plugged into the calculation of the output. I don’t get WHY this is or how the function decides to handle this exactly.

When someone later calls: “triple(2);” How is it that it returns 6?? Why doesn’t the 3 get replaced by the 2? Can somebody step by step the logic on this for me as far as what happens to the original 3 value?

1 Like

Hey @CryptoEatsTheWorld, hope you are great.

About your code, that is not the best approach to build a function, in this case, there is no point to have a nested function, also it could be bad practice, hard to maintain or improve at long run.

Try to keep your functions simple, easy to read and understand, for the multiply function you don’t need another function in the function body of your first one.
Example:

function multiple(x, y){
   return x * y
}
//two ways to use it, first way
var oneWay = multple(3, 3) //returns 9
//second way
var a = 3
var b = 3
var result = multiple(a, b) //returns 9

Problem in your code is that you are creating a function that start with 1 parameter (multiple require x), inside of this one, another function fn with the other parameter (y), you describe the logic, then the first one return “fn”, but it should be written has fn(number) because your function require one parameter to work with.

Hope you find this useful.
If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Carlos,

I think you misunderstood. I did NOT write this code. This code was include in a page that the Javascript course here tells me to read. I agree it is confusing. I am trying to understand how it works because it was provided as an example.

The code works. I wanted someone to explain HOW it works.

Chris

1 Like

Ok, my bad man, let me explain it to you, it is complete ok to use it, but it can be confusing at the long run.

/* multiply 2 numbers
// 'fn' function will be invoked through a binded variable
// 
*/
function multiple(x) { //get 1 parameter 'x'

    function fn(y) //internal function, used to multiply 2 numbers
    {
        return x * y; //return the result of the operation
    }
    return fn; //return the value 
}

var triple = multiple(3); // assign the function to a variable with value 3
//invoke the internal function 'fn', human read will be 3(2) which is 6
//3(2) is fn(2), you are sending a parameter to the internal function of the 'triple' variable
triple(2); // returns 6 
triple(3); // returns 9

Hope you find this useful.
If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Yeah I do understand the statements in the code. My question is:

When this statement is run, for example: “triple(2)”

Normally you would expect this:

function multiple(2) { // 2 is pulled into the function

    function fn(y) 
    {
        return 2 * y; // 2 replaces x
    }
    return fn; //return the value 
}

How is it that 3 ends up replacing y and then ends up in the displayed result? by what logic does the function know to do that with the number 3 that was passed when the function was instantiated?

Or are you saying that when the call attempts to pass in the 2 to multiplier, that it instead sends “3(2)” as x and it ignores y? I don’t think so, because multiplying by y would cause an error because it is undefined.

What if the equation in fn(y) was y * a * x? What happens in that scenario? Does 3 get assigned to every otherwise undefined variable?