将两个数字相加JS

时间:2018-06-22 12:28:39

标签: javascript arrays

我想添加10-99范围内的两个数字,例如:

function digital_root(n) {
  var z = n.toString().length;
  if (z == 2) {
    var x = z[0] + z[1]
    return x;
  }
}

console.log( digital_root(16) );

{{1}}

此代码的输出为NaN。我应该纠正什么?

8 个答案:

答案 0 :(得分:2)

您可以尝试以下方法:

function digital_root(n) {
  var z = n.toString();
  //use length here
  if (z.length == 2) {
    //convert to int
    var x = parseInt(z[0]) + parseInt(z[1]);
    return x;
  } else {
    return "not possible!";
  }
}

console.log( digital_root(16) );
console.log( digital_root(99) );
console.log( digital_root(999) );

答案 1 :(得分:2)

使用split将字符串分成两半,然后使用parseInt将两者相加以转换为数字。

const sum = (s) => (''+s).split('').reduce((a,b) => parseInt(a)+parseInt(b))
       ↑             ↑        ↑         ↑
      our          coerce   split      sum
    function     to string  in two     both

在这里进行测试:

const sum = (s) => (''+s).split('').reduce((a,b) => parseInt(a)+parseInt(b))

console.log(sum(12))

答案 2 :(得分:1)

有几种方法可以求和一个数字。您可以将其转换为字符串,但也可以将其转换为IDK。您可以通过数字运算来做到这一点。

var input = 2568,
    sum = 0;

while (input) {
    sum += input % 10;
    input = Math.floor(input / 10);
}

console.log(sum);

答案 3 :(得分:1)

这是一个有趣的简短方法:

const number = 99
const temp = number.toString().split('')
const res = temp.reduce((a, c) => a + parseInt(c), 0) // 18

1。)将数字转换为字符串

2。)分隔成单个数字

3。)使用reduce对数字求和。

答案 4 :(得分:1)

您的方法将是解决此问题的迭代方法,但是您也可以使用递归方法。

迭代解决方案(必须)

  1. n.toString()根据数字创建字符串。
  2. .split("")将字符串拆分为字符。
  3. .reduce(callback, startValue)通过将回调函数应用于每个元素并更新startValue,将数组减少为单个值。
  4. (s, d) => s + parseInt(d)回调函数,它将元素解析为整数并将其添加到s(startValue)中。
  5. 0 startValue。

递归解决方案(功能性)

  1. condition?then:else简写(如果使用符号)。
  2. n<10仅一位数字=>只需返回即可。
  3. n%10当前数字的最后一位(1234%10 = 4)。
  4. digital_root_recurse(...)递归调用该函数。
  5. Math.floor(n / 10)除以10 =>将小数点向左移动(1234 => 123)
  6. ... + ...添加n/10的最后一位数字和返回值(数字根)(1234 => 4 + root(123))。

function digital_root_string(n) {
  return n.toString().split("").reduce((s, d) => s + parseInt(d), 0);
}

function digital_root_recurse(n) {
  return n < 10 ? n : n % 10 + digital_root_recurse(Math.floor(n / 10));
}

console.log(digital_root_string(16));
console.log(digital_root_string(99));
console.log(digital_root_recurse(16));
console.log(digital_root_recurse(99));

答案 5 :(得分:0)

代码中的问题是您将n的长度存储到z中。长度是整数,因此z[0][1]均未定义。解决方案是将字符串存储到另一个变量中,并使用它代替z

function digital_root(n) {
  n = n.toString();
  var l = n.length;
  if (l === 2) {
    return parseInt(n[0], 10) + parseInt(n[1], 10);
  }
}

console.log( digital_root(16) );

答案 6 :(得分:0)

只需使用var x = parseInt(n/10) + (n%10);,它将为您服务。

function digital_root(n) {
  var z = n.toString().length;
  if (z == 2) {
    var x =  parseInt(n/10) + (n%10);
    return x;
  }
}

console.log( digital_root(16) );
console.log( digital_root(99) );
console.log( digital_root(62) );

答案 7 :(得分:0)

将输入转换为字符串,将其拆分,将每个项目转换回数字,然后将它们全部求和:

function digital_root(n) {
    return String(n).split('').map(Number).reduce((a,b) => a + b)
}

const result = digital_root(99);

console.log(result);
相关问题