kunak54
kunak54

Reputation: 11

Adding two numbers JS

I want to add two numbers from range 10-99,for example:

Input:16
Output:1+6=7
Input:99
Output:18

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

Output from this code is NaN.What should I correct?

Upvotes: 0

Views: 270

Answers (8)

Leonid Pyrlia
Leonid Pyrlia

Reputation: 1712

Convert input to string, split it, convert each item back to number and sum them all:

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

const result = digital_root(99);

console.log(result);

Upvotes: 0

Sebastian Speitel
Sebastian Speitel

Reputation: 7346

Your way would be the iterational way to solve this problem, but you can also use a recursive way.

Iterative solution (Imperative)

  1. n.toString() Create String from number.
  2. .split("") split string into chars.
  3. .reduce(callback, startValue) reduces an array to a single value by applying the callback function to every element and updating the startValue.
  4. (s, d) => s + parseInt(d) callback function which parses the element to an integer and adds it to s (the startValue).
  5. 0 startValue.

Recursive solution (Functional)

  1. condition?then:else short-hand if notation.
  2. n<10 only one digit => just return it.
  3. n%10 the last digit of the current number (1234%10 = 4).
  4. digital_root_recurse(...) call the function recursivly.
  5. Math.floor(n / 10) Divide by 10 => shift dcimal point to left (1234 => 123)
  6. ... + ... add the last digit and the return value (digital root) of 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));

Upvotes: 1

Ivan
Ivan

Reputation: 40638

Use split to split the string in half and add the two using parseInt to convert to a number.

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

Here a test :

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

console.log(sum(12))

Upvotes: 2

Colin Ricardo
Colin Ricardo

Reputation: 17239

Here's a fun short way to do it:

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

1.) Convert number to string

2.) Separate into individual numbers

3.) Use reduce to sum the numbers.

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Simply use var x = parseInt(n/10) + (n%10); and it will work for you.

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

Upvotes: 0

Luuuud
Luuuud

Reputation: 4439

The issue in your code is that you stored the length of n into z. The length is an integer, so both z[0] and [1] are undefined. The solution is to store the string into another variable and use that instead of 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) );

Upvotes: 0

Cristian S.
Cristian S.

Reputation: 973

There are several approaches to sum digits of a number. You can convert it to a string but IDK if thats neccesary at all. You can do it with numerical operations.

var input = 2568,
    sum = 0;

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

console.log(sum);

Upvotes: 1

Temani Afif
Temani Afif

Reputation: 272789

You can try this:

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

Upvotes: 2

Related Questions