Total no of 1 's in numbers | Javascript code to count the frequency of 1 in numbers less than or equal to the given number . ; Driver Code
function countDigitOne ( n ) { let countr = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let str = i . toString ( ) ; countr += str . split ( " " ) . length - 1 ; } return countr ; } let n = 13 ; document . write ( countDigitOne ( n ) + " " ) ; n = 131 ; document . write ( countDigitOne ( n ) + " " ) ; n = 159 ; document . write ( countDigitOne ( n ) + " " ) ;