Fascinating Number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not
function isFascinating ( num ) { let freq = new Array ( 10 ) ; for ( let i = 0 ; i < freq . length ; i ++ ) { freq [ i ] = 0 ; } let val = " " + num + num * 2 + num * 3 ; for ( let i = 0 ; i < val . length ; i ++ ) { let digit = val [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( freq [ digit ] > 0 && digit != 0 ) return false ; else freq [ digit ] ++ ; } for ( let i = 1 ; i < freq . length ; i ++ ) { if ( freq [ i ] == 0 ) return false ; } return true ; } let num = 192 ; if ( num < 100 ) document . write ( " " ) ; else { let ans = isFascinating ( num ) ; if ( ans ) document . write ( " " ) ; else document . write ( " " ) ; }