Check if a number is in given base or not | Javascript program to check if given number is in given base or not . ; Allowed base are till 16 ( Hexadecimal ) ; If bas is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver Code
function isInGivenBase ( str , bas ) { if ( bas > 16 ) return false ; else if ( bas <= 10 ) { for ( let i = 0 ; i < str . length ; i ++ ) if ( ! ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) < ( ' ' . charCodeAt ( ) + bas ) ) ) return false ; } else { for ( let i = 0 ; i < str . length ; i ++ ) if ( ! ( ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) < ( ' ' . charCodeAt ( ) + bas ) ) || ( str [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str [ i ] . charCodeAt ( ) < ( ' ' . charCodeAt ( ) + bas - 10 ) ) ) ) return false ; } return true ; } let str = " " ; if ( isInGivenBase ( str , 16 ) == true ) document . write ( " " ) ; else document . write ( " " ) ;