Find the sum of digits of a number at even and odd places | Function to find the sum of the odd and even positioned digits in a number ; If n is odd then the last digit will be odd positioned ; To store the respective sums ; While there are digits left process ; If current digit is odd positioned ; Even positioned digit ; Invert state ; Remove last digit ; Driver code
function getSum ( n ) { let isOdd = ( n % 2 == 1 ) ? true : false ; let sumOdd = 0 , sumEven = 0 ; while ( n != 0 ) { if ( isOdd ) sumOdd += n % 10 ; else sumEven += n % 10 ; isOdd = ! isOdd ; n = Math . floor ( n / 10 ) ; } document . write ( " " + sumOdd + " " ) ; document . write ( " " + sumEven ) ; } let n = 457892 ; getSum ( n ) ;