Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sisterAn opened this issue Sep 4, 2020 · 8 comments
Open

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

sisterAn opened this issue Sep 4, 2020 · 8 comments
Labels

Comments

@sisterAn
Copy link
Owner

sisterAn commented Sep 4, 2020

例如:

add(1)(2,3)(4).value()   
输出: 10
@sisterAn sisterAn added the 字节 label Sep 4, 2020
@kexiaofu
Copy link

kexiaofu commented Sep 4, 2020

先来抛砖,希望可以引玉

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

@oxyzhg
Copy link

oxyzhg commented Sep 4, 2020

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;
}

@m7yue
Copy link

m7yue commented Sep 4, 2020

思路都差不多

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()

@feng9017
Copy link

feng9017 commented Jan 4, 2021

function add(...params) {
    const result = add.bind(null, ...params);
    result.value = () => params.reduce((accumulator, cur) => accumulator + cur);

    return result;
}

@z253573760
Copy link

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);

@dlrandy
Copy link

dlrandy commented Jun 8, 2021

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;
}

@xllpiupiu
Copy link

/**
 * 函数柯里化实现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)

@liu-mengwei
Copy link

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
Labels
Projects
None yet
Development

No branches or pull requests

9 participants