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

2020-03-12 实现add方法 #7

Open
fanmingfei opened this issue Mar 12, 2020 · 8 comments
Open

2020-03-12 实现add方法 #7

fanmingfei opened this issue Mar 12, 2020 · 8 comments

Comments

@fanmingfei
Copy link
Member

function add(number) {
}

add(1)(2)() // 3, 1+2 = 3 

add(1)(2)(3)() // 6, 1+2+6 =6

add(5)(8)(8)(6)(5)() // 32,5+8+8+6+5 = 32
// ...  以此类推,后面的执行次数不限

实现 该 add 方法

@Vince-9
Copy link

Vince-9 commented Mar 12, 2020

function add(v) {
  if(v === undefined) return 0;
  let sum = v;
  return function(val){
    if(val === undefined) return sum;
    return add(val + sum);
  }
}

@xiaosen7
Copy link

const add = (n, S = n) => typeof n !== 'undefined' ? m => add(m, isNaN(m) ? S :S+m) : S;

@jef0317
Copy link

jef0317 commented Mar 12, 2020

function add(args){
return function(num) {
if(num){
return add(args+num)
}else{
return args
}
}
}

@joker1148
Copy link

function add(v){
    if(v === undefined) return;
    else{
        let sum = v
        return (val)=>{
            if(val === undefined) return sum
            return add(sum+val)
        }
    } 
}
console.log(add(1)(2)(3)().toString())
借鉴了一下下哈哈

@SunYiwen
Copy link

function add(num) {
    let sum;
    if(arguments.length === 0) {
        sum=0;
        return sum;
    }
    else{
        sum = num;
        return function innerAdd(number) {
            if(arguments.length === 0){
                return sum;
            }
            else{
                return add(number + sum);
            }
        }
    }
}

@LiZhaji
Copy link

LiZhaji commented Mar 12, 2020

function add(number){
  if (number!== 0 && !number) return 0
  return function (num) {
    if (num) return add(number + num)
    return number
  }
}

@fanmingfei
Copy link
Member Author

function add(number = 0){
  return function add2(num) {
    number += num || 0
    if (num===undefined) return number
    return add2
  }
}

@liuyingbin19222
Copy link

function add(num){
    var sum = 0;
    sum = sum + num;
    return function Fun(numB){
        if(arguments.length === 0){
            return sum;
        }else {
            sum = sum + numB;
            return Fun;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants