Skip to content

字节编程题:实现一个add方法 #103

Open

Description

@sisterAn

例如:

add(1)(2,3)(4).value()   
输出: 10

Activity

  1. kexiaofu commented on Sep 4, 2020

    @kexiaofu

    先来抛砖,希望可以引玉

    function add() {
      const args = Array.prototype.slice.apply(arguments);
      const self = this;
      this.nums = [...args];
      function _add() {
        const _args = Array.prototype.slice.apply(arguments);
        this.nums.push(..._args);
        return _add; 
      }
      _add.value = function() {
        return self.nums.reduce((acc, cur) => acc += cur, 0);
      }
      return _add;
    }

    考验的是闭包和this

  2. oxyzhg commented on Sep 4, 2020

    @oxyzhg
    function add(...args) {
      const nums = [...args];
    
      function addFn(...args1) {
        nums.push(...args1);
        return addFn;
      }
    
      addFn.value = () => {
        const sum = nums.reduce((s, n) => s + n, 0);
        console.log(sum);
        return sum;
      };
    
      return addFn;
    }
  3. m7yue commented on Sep 4, 2020

    @m7yue

    思路都差不多

    const add = (...args) => {
        const _add = (...args1) => {
            return add(...args, ...args1)
        }
        _add.value = () => args.reduce((t, e) => t+e)
    
        return _add
    }
    add(1)(2,3)(4).value()
  4. feng9017 commented on Jan 4, 2021

    @feng9017
    function add(...params) {
        const result = add.bind(null, ...params);
        result.value = () => params.reduce((accumulator, cur) => accumulator + cur);
    
        return result;
    }
    
  5. z253573760 commented on Feb 2, 2021

    @z253573760
    function add(...args) {
      const _add = (..._args) => {
        args.push(..._args);
        return _add;
      };
      _add.value = () => args.reduce((a, b) => a + b, 0);
      return _add;
    }
    
    const res = add(1)(2, 3)(4).value(); //10
    console.log(res);
    
  6. dlrandy commented on Jun 8, 2021

    @dlrandy

    function add(...params) {
    let args = [...params];
    function innerAdd(...p) {
    args = args.concat(p);
    return innerAdd;
    }
    function value() {
    return args.reduce((s, n) => s + n , 0)
    }
    innerAdd.value = value;

    return innerAdd;
    }

  7. xllpiupiu commented on Oct 1, 2021

    @xllpiupiu
    /**
     * 函数柯里化实现add(1)(1,2)(3)等
     * https://blog.csdn.net/weixin_30498807/article/details/102319249
     */
    // let obj = {
    //     name:'hell',
    //     toString:function() {
    //         console.log('调用了toString')
    //         return '22'
    //     },
    //     valueOf:function() {
    //         console.log('调用了obj.valueOf');
    //         return '00'
    //     }
    // }
    // console.log(obj+"3")
    function test() {
        console.log(1);
    }
    test.toString = function () {
        console.log('调用了valueOf方法');
        return 2;
    }
    function add() {
        var arr = Array.prototype.slice.call(arguments)
        const _adder = function () {
            arr.push(...arguments)
            return _adder;
        }
        _adder.valueOf = function () {
            return arr.reduce((a, b) => a + b);
        }
        return _adder;
    }
    let res = add(1)(2)(3, 4);
    console.log(add(1)(2, 3).valueOf())
    console.log(res+0)
  8. liu-mengwei commented on Dec 6, 2022

    @liu-mengwei

    function add(...args1) {
    let _add = function (...args2) {
    return add(...args1, ...args2);
    };

    _add.value = function () {
    let sum = 0;
    for (let num of args1) {
    sum += num;
    }
    return sum;
    };

    return _add;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions