0

I'm a new student who's learning Javascript for the first time. This time I'm trying to better grasp the concepts of converting numbers into strings, storing them in arrays, converting them back to numbers, and adding.

In this assignment, I'm trying to write a function that takes the individual digits of a number and adds them together.

So for example, the function would take (95) and return 14. Or given (135), would return 9.

Here's what I got so far:

var addDigits = function(num) {
  var newNum = num.toString();
  newNum = newNum.split('');
  var sum = 0;
  var thirdNum = newNum.forEach(function(x) {
      parseInt(x);
sum + x };
};

I'm fully aware that is not very good code, but could anyone give me any tips? Should I be using parseInt or Number?

royhowie
  • 11,075
  • 14
  • 50
  • 67
Leia_Organa
  • 1,894
  • 7
  • 28
  • 48

5 Answers5

3

You're pretty close. Few things though. array.forEach doesn't return anything. It's used for creating side effects (increasing sum would be considered a side effect of the function you're passing into the forEach). So setting the forEach to a variable doesn't accomplish anything. parseInt does return something, so you need to set it to a variable. And you also want to increase sum by the parsed integer plus the sum you already have. You can look into the += operator for that if you wish. Last, you need to return a value from the function itself! As it is, if you did var added = addDigits(123), added would be undefined. So finish it off with a return statement.

After you've got the grasp of that, I'd suggest looking into array.reduce to replace array.forEach since it's perfect for a problem such as this.

royhowie
  • 11,075
  • 14
  • 50
  • 67
kevrom
  • 156
  • 6
1
var addDigits = function(string) {
  newNum = string.split('');
  var sum = 0;
  for(var i = 0 ; i < newNum.length ; i++) sum += parseInt(newNum[i]);
  return sum;
};

console.log(addDigits("1234"));
jeffmurphy
  • 446
  • 2
  • 11
1

Maybe this will be better:

function getDigitsSum(number) {
  var charArray = (number + '').split('');
  var sum = 0;
  charArray.forEach(function(item) {
    sum += parseInt(item)
  })
  return sum;
}
David Guan
  • 4,180
  • 1
  • 25
  • 32
1

Number() performs type conversion, whereas parseInt() performs parsing.
What is the difference between parseInt() and Number()?
In this situation, it doesn't make a difference since the strings are proper integers.
Here's how I would do it.

var addDigits = function(num) {
  var newNum = num.toString().split('');
  var sum = newNum.map(Number).reduce((prev, curr) => prev + curr);
  return sum;
};  

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Community
  • 1
  • 1
Prashanth Chandra
  • 2,672
  • 3
  • 24
  • 42
1

Assuming your input will always be an integer string, consider the following function:

var addDigits = function(strInt) {
    var int = function(x) {
        return parseInt(x,10) //10 is the radix 
    }
    return strInt.split('').map(int).reduce(function(a,b){return a+b}); 
}

The function tied to var int will ensure that the provided integer string be parsed into its corresponding base 10 integer (notice the difference in type, which can be validated with Javascript's built-in typeof() function). The return will first .split the string, .map the int function against every value within the given string, and then apply whatever function you have within .reduce against an accumulated value - in this case, simply adding against each member of the array.

Xenyal
  • 2,136
  • 2
  • 24
  • 51