DEV Community

Discussion on: Turning 38 into 2: How to Solve the Add Digits Problem

Collapse
 
kebby profile image
Tammo 'kb' Hinrichs

The fun part about the digit sum is that it behaves like a modulo with 9, so this here works:

const digitSum = n => n<10 ? n : (n-1) % 9 + 1;

Collapse
 
salyadav profile image
Saloni Yadav

Hey man! This is new, I see this works! Could you please explain (or refer to) the math behind this or how this works as a modulo 9? Thanks!

Collapse
 
kebby profile image
Tammo 'kb' Hinrichs

I can’t really explain it in a few words but I hope this helps: flyingcoloursmaths.co.uk/a-neat-nu... :)

Collapse
 
sagarb3 profile image
Sagar Bhattacharya

Brilliant Solution , Math Saves the day !