Harshad ( Or Niven ) Number | Javascript implementation of above approach ; Converting integer to string ; Initialising sum to 0 ; Traversing through the string ; Converting character to int ; Comparing number and sum ; Driver Code ; passing this number to get result function
function checkHarshad ( n ) { let st = String ( n ) let sum = 0 let length = st . length for ( i in st ) { sum = sum + parseInt ( i ) } if ( n % sum == 0 ) { return " " } else { return " " } } let number = 18 document . write ( checkHarshad ( number ) )