0

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?

8
  • 1
    z is a number here, not a list or a string. So you are essentialy doing undefined + undefined which correctly outputs NaN Commented Jun 22, 2018 at 12:29
  • 2
    should it be n[0] and n[1] ? Commented Jun 22, 2018 at 12:30
  • don't use .length there. Commented Jun 22, 2018 at 12:31
  • 1
    I don't want to use libraries :) So in this if i should convert it to a string again? Commented Jun 22, 2018 at 12:33
  • 2
    @TemaniAfif, did you really say that? Commented Jun 22, 2018 at 12:37

8 Answers 8

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

4 Comments

Would you care to explain why you downvoted? Without a comment you're vote has no way of improving my answer.
@GuillaumeGeorges, thank you for the feedback, I have edited my answer.
I didnt vote down but the readability of your code is not optimal IMHO.
@CristianS. yes, it is quite condensed but for a function that short there is no need to break it down even further. Downvoting without a comment is nonsense.
1

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

Comments

1

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.

Comments

1

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

3 Comments

What does applicative mean in this context?
Sorry, it's an translation mistake of me. It has to be functional
Ah, was just curious! :)
0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.