Check if a number is magic ( Recursive sum of digits is 1 ) | JavaScript program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code
function isMagic ( n ) { var sum = 0 ; while ( n > 0 sum > 9 ) { if ( n = 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum = 1 ) ; } var n = 1234 ; if ( isMagic ( n ) ) document . write ( " " ) ; else document . write ( " " ) ;