 # Can I make this more concise?

I have solved an algorithm but I’m looking for tips on how I might shorten the code up.
The basic problem is separate a given integer into its respective digits, add those digits together and subtract that sum from the initial integer.
Collect those sums into an array and determine the most frequent number within that array. (If several frequent numerals, then choose highest integer.)
Return that most frequent number.

Below is my code. If you can give me some tips on how it can be shrunk, then I would really appreciate it!

``````function mostFrequentDigitSum(n: number): number {
//create while loop that runs into initial integer is 0
while(n > 0){
//use container INT to hold integer in string form to split numeral of two digits
let int = n.toString().split('');
//break up double digit number into two variables
let digitOne = parseInt(int);
let digitTwo = parseInt(int);
//create container ONE for numbers generated with cycle of subtraction from initial integer
let arrOne: number[] = [];
//create container TWO for initial integers generated with each cycle of the loop
let arrTwo: number[] = [];
//create container NUM for addition of individual digits in double digit numbers
let num;
//if statement for double digits to add respective digits together&&push to ONE container
if(int.length>1){
num = digitOne+digitTwo
arrOne.push(num)
//when only single digit return
} else {
return digitOne
}
//create container RESULT for subtracting sum within NUM from initial integer N
let result = n - num;
//create container TWO for new digits at completion of loop cycle
arrTwo.push(result)
//change value of N to RESULT and begin new cycle
n = result;
}
}
console.log(mostFrequentDigitSum(88)); 9
console.log(mostFrequentDigitSum(8)); 8

``````
1 Like

I’m not quite sure I understand the question as the challenge said “Not Authorized” when I clicked on it, but here is a better way to extract digits and sum them:

``````let num = 12345;
let sum = 0;

console.log(num);

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

console.log(sum);
console.log(num);

``````

See if you can figure out what the code above is doing. Let me know if you have any questions.

1 Like

As far as figuring the sum goes, there’s also a great fat-arrow option:

• convert the number to a string (using `.toString()`).
• strings can be easily converted to an array, using the spread operator.
• `.reduce()` the array, to get the sum of the digits.

Here’s how it would look, quick and dirty:

``````// This will be used inside the reduce function
const sum = (accumulator, val) => accumulator+Number(val);
// Longhand, that would look like this:
/***
* const sum = function(accumulator, val){
*    // convert the digit back to a number, and add it to our accumulator
*   return accumulator+Number(val);
* };
***/

// And here, we convert a number to a string, which is an "array-like structure",
//  and then split the string into an array. Then we can use reduce on that array,
//  to get a final sum.
const sumFromString = (num) => [...num.toString()].reduce(sum, 0);
/***
* Again, that fat arrow function can be expanded out:
* const sumFromString = function(num){
*   const numStr = num.toString();  // turn the number to a string, as that's easily converted.
*   const arrayOfDigits = [...numStr];  // take the string, and spread it into an array.
*   return arrayOfDigits.reduce(sum, 0);  // use the array reduce method to accumulate a sum!
* };
***/

console.log(sum(12345));
``````
1 Like