1. Computing & Technology

Adding Digits Together

Suppose that you have a long string of numbers where you need to add all the digits together and then add the digits of the results together until such time as you get it down to a single digit. This might need to be done for a number of reasons such as to calculate whether a number containing a check digit is valid. Usually a check digit calculation will be slightly more involved than just a straight addition of all the numbers since we also need to be able to tell if adjacent digits have been accidentally reversed. We'll start with a simple function to just add all the digits to get to a single digit answer and then we'll look at how you might modify the code to handle specific checkdigit processing by looking at where we multiply every second digit in the original number by two as we add them.

Here's our straight addition function.

function addDigits(s) {
var l = s.length;
var w = 0;
for (var i=0; i < l; i++) {
w += +s.charAt(i);
}
if (w > 9) w = addDigits(w.toString());
return w;
}

The value passed into this function is assumed to be a valid text string containing nothing but numbers so you'd need to add the validation for that first. The first line in the function stores the length of the string so we don't have to constantly reference it each time around the loop and the third line sets up our results field and sets it to zero. The loop simply extracts each character from the input string in turn, converts it to a number and then adds it to the result. If the result is greater than 9 we then conver the result back into a text string and call the function again.

To allow for multiplying every second digit by two so as to use this for a checkdigit routine we simply need to add a second arguement to the function that determines whether to multiply every second digit by two. We set this value to true or 1 in order to multiply every second digit by two and set it to false or 0 to not multiply by two. This allows us to cater for not wanting to start multiplying the digits in the result by two when we add all those digits together.

Our revised function looks like this:

function addDigits(s,d) {
var l = s.length;
var w = 0;
for (var i=0; i < l; i++) {
if (d && i%2) w += s.charAt(i) * 2;
else w += +s.charAt(i);
}
if (w > 9) w = addDigits(w.toString(),0);
return w;
}

The only difference that there is between this second function and the first is that with this second version if we pass in 1 as the second parameter then every second digit in the original number will be multiplied by two before adding it to the result. When the digits in the result are added together to bring it down to a single digit the numbers will just be added without multiplying any of them by two.

Let's say we have a 16 digit number where the last digit is a check digit calculated in this way. To verify that the number is correct we first remove the check digit and feed the rest of the number into our function. If the number is correct then the single digit returned should be the same as the check digit that we stripped off. Reversing any two digits would give a different result and confirm that the number had been mistyped. (Note the calculation for credit card numbers is slightly different from this in that the check digit will be ten less the number this function returns). So if we run addDigits('164723456789332',1) we get 2 returned.

©2012 About.com. All rights reserved.

A part of The New York Times Company.