{"size": 1854, "ext": "js", "max_stars_repo_path": "leetcode/8 stringToInteger.js", "max_stars_repo_name": "fanbyprinciple/coding_interview_questions", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "leetcode/8 stringToInteger.js", "max_issues_repo_name": "fanbyprinciple/coding_interview_questions", "max_issues_repo_issues_event_min_datetime": "2020-08-17T06:40:46.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-17T03:30:02.000Z", "max_forks_repo_path": "leetcode/8 stringToInteger.js", "max_forks_repo_name": "fanbyprinciple/coding_interview_questions", "max_forks_repo_forks_event_min_datetime": "2020-08-18T03:01:19.000Z", "max_forks_repo_forks_event_max_datetime": "2020-08-18T03:01:19.000Z"}
{"max_stars_count": null, "max_issues_count": 9, "max_forks_count": 1, "avg_line_length": 29.9032258065, "max_line_length": 104, "alphanum_fraction": 0.4066882416}
// Source : https://leetcode.com/problems/string-to-integer-atoi/submissions/
// Runtime: 148 ms, faster than 14.01% of JavaScript online submissions for String to Integer (atoi).
// Memory Usage: 43.3 MB, less than 5.02% of JavaScript online submissions for String to Integer (atoi).

var myAtoi = function(str) {
    numberArray = "1234567890"
    sign = "-+"
    processed = []
    for(let i=0; i <str.length; ++i){

        if(!(str[i] == " " && processed.length ==0) ){
            
            if(numberArray.includes(str[i]) || (sign.includes(str[i])&&processed.length==0)){
                console.log(str[i])        
                processed.push(str[i])
                
                
            } else {
                
                if(processed.length == 0){
                    return 0
                } else if(processed.length == 1 && sign.includes(processed[0])) {
                    return 0
                } else {
                    
                    let res = parseInt(processed.join(""))
                    if(res > 2**31-1){
                        res = 2**31-1
                    } else if (res < -(2**31)){
                        res = -(2**31)
                    } 
                             
                    //console.log(processed.join(""))
                    return res
                    
                }
            }
        
        }
    }

    if(processed.length == 0){
        return 0
    } else if(processed.length == 1 && sign.includes(processed[0])) {
        return 0
    } else {
        
        let res = parseInt(processed.join(""))
        if(res > 2**31-1){
            res = 2**31-1
        } else if (res < -(2**31)){
            res = -(2**31)
        } 
                 
        //console.log(processed.join(""))
        return res
        
    }
    
};

// myAtoi("-")