Check if a given number is Fancy | javascript program to find if a given number is fancy or not ; To store mappings of fancy pair characters . For example 6 is paired with 9 and 9 is paired with 6. ; Find number of digits in given number ; Traverse from both ends , and compare characters one by one ; If current characters at both ends are not fancy pairs ; Driver Code
function isFancy ( num ) { var fp = new Map ( ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; fp . set ( ' ' , ' ' ) ; var n = num . length ; var l = 0 , r = n - 1 ; while ( l <= r ) { if ( ! fp . has ( num . charAt ( l ) ) || fp . get ( num . charAt ( l ) ) != num . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } var str = " " ; if ( isFancy ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;