Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript method with both options multiple(x,y) multiple(x)(y)

    Hallo ihr lieben,

    Hallo


    I try to make it possible that the method muliply
    works both ways:
    multiply(2)(4) multiply(2,4)

    If I code them seperatly they would look like add1 and add2:

    function add1(x,y){
    return x + y;
    }
    function add2(x) {
    return function(y) {
    return x + y;
    };
    }

    Now I want to combine the two.

    That´s my first try. With an optional parameter.

    function add (x,y){
    if(!y){ return x + function(x){
    return function(y) {
    return x * y;
    };
    }
    }
    return x*y;
    }

    But anything isn´t right here.

    If somebody would help me it would be great!

  • #2
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry I mixed up to things. Every add should meen multiply...

  • #3
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Where can I edit the thread?

    Right would be:


    I try to make it possible that the method muliply
    works both ways:
    multiply(2)(4) multiply(2,4)

    If I code them seperatly they would look like add1 and add2:

    function add1(x,y){
    return x*y;
    }
    function multiply2(x) {
    return function(y) {
    return x*y;
    };
    }

    Now I want to combine the two.

    That´s my first try. With an optional parameter.

    function multiply (x,y){
    if(!y){ return x + function(x){
    return function(y) {
    return x * y;
    };
    }
    }
    return x*y;
    }

    But anything isn´t right here.

    If somebody would help me it would be great!

  • #4
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for post


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •