Write your own atoi ( ) | A simple JavaScript program for implementation of atoi ; if whitespaces then ignore . ; sign of number ; checking for valid input ; handling overflow test case ; Driver code ; Function call
function myAtoi ( str ) { var sign = 1 , base = 0 , i = 0 ; while ( str [ i ] == ' ' ) { i ++ ; } if ( str [ i ] == ' ' str [ i ] == ' ' ) { sign = 1 - 2 * ( str [ i ++ ] == ' ' ) ; } while ( str [ i ] >= ' ' && str [ i ] <= ' ' ) { if ( base > Number . MAX_VALUE / 10 || ( base == Number . MAX_VALUE / 10 && str [ i ] - ' ' > 7 ) ) { if ( sign == 1 ) return Number . MAX_VALUE ; else return Number . MAX_VALUE ; } base = 10 * base + ( str [ i ++ ] - ' ' ) ; } return base * sign ; } var str = " " ; var val = myAtoi ( str ) ; document . write ( " " , val ) ;