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

实现(5).add(3).minus(2)功能 #115

Open
Sunny-117 opened this issue Nov 3, 2022 · 4 comments
Open

实现(5).add(3).minus(2)功能 #115

Sunny-117 opened this issue Nov 3, 2022 · 4 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@mengqiuleo
Copy link

(function() {
  function check(x) { //统一转成数字
     return x = isNaN(x) ? 0 : x - 0
  }

  function add (x) {
     x = check(x)
     return this + x
  }

  function minus (x) {
     x = check(x)
     return this - x
  }
  
  Number.prototype.add = add

  Number.prototype.minus = minus    
})();

console.log((5).add('12').minus('1')) // 16
console.log((5).add(2))  // 7
console.log((5).add('abc').minus('1')) // 4

@bearki99
Copy link

Number.prototype.add = function(num) {
    return this + (+num);
}
Number.prototype.minus = function(num){
    return this - (+num);
}
console.log((5).add(3).minus(2));

@veneno-o
Copy link
Contributor

Number.prototype.add = function(...numbs){
    let res = this;
    for (const num of numbs) {
        res += (~~num);
    }
    return res;
}
Number.prototype.minus = function(...numbs){
    let res = this;
    for (const num of numbs) {
        res = Math.min(this, (~~num));
    }
    return res;
}

@kangkang123269
Copy link

Number.prototype.add = function(num) {
    return this.valueOf() + num;
};

Number.prototype.minus = function(num) {
    return this.valueOf() - num;
};

console.log((5).add(3).minus(2)); // 输出 6

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

5 participants