两个数相加

/**
 * Adds two numbers
 * 两个数相加
 * @param {number} augend The first number in an addition
 * @param {number} addend The second number in an addition
 * @returns {number} Returns the total
 * @example
 * add(6, 4)
 * // => 10
 * add(6, '4')
 * // => '64'
 */

import createMathOperation from "./createMathOperation"

var add = createMathOperation(function(augend, addend) {
  return augend + addend
}, 0)

export default add
/**
 * Creates a function that performs a mathematical operation on two values
 * @param {Function} operator The function to perform the operation
 * @param {number} [defaultValue] The value used for `undefined` arguments
 * @returns {Function} Returns the new mathematical operation function
 */
import baseToString from "./baseToString"
import baseToNumber from "./baseToNumber"

function createMathOperation(operator, defaultValue) {
  return function(value, other) {
    var result
    if (value === undefined && other === undefined) {
      return defaultValue
    }
    if (value !== undefined) {
      result = value
    }
    if (other !== undefined) {
      if (result === undefined) {
        return other
      }
      if (typeof value === "string" || typeof other === "string") {
        value = baseToString(value)
        other = baseToString(other)
      } else {
        value = baseToNumber(value)
        other = baseToNumber(other)
      }
      result = operator(value, other)
    }
    return result
  }
}

export default createMathOperation

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值