String to Integer (atoi) I was doing following question on leetcode: String to Integer (atoi)

Question: 


  Implement atoi which converts a string to an integer.
  
  The function first discards as many whitespace characters as necessary
  until the first non-whitespace character is found. Then, starting from
  this character, takes an optional initial plus or minus sign followed
  by as many numerical digits as possible, and interprets them as a
  numerical value.
  
  The string can contain additional characters after those that form the
  integral number, which are ignored and have no effect on the behavior
  of this function.
  
  If the first sequence of non-whitespace characters in str is not a
  valid integral number, or if no such sequence exists because either
  str is empty or it contains only whitespace characters, no conversion
  is performed.
  
  If no valid conversion could be performed, a zero value is returned.
  
  Note:
  
  Only the space character ' ' is considered as whitespace character.
  Assume we are dealing with an environment which could only store
  integers within the 32-bit signed integer range: [−231,  231 − 1]. If
  the numerical value is out of the range of representable values,
  INT_MAX (231 − 1) or INT_MIN (−231) is returned.


Question link: https://leetcode.com/problems/string-to-integer-atoi/

For the above question, I wrote the following code 

/**
 * @param {string} str
 * @return {number}
 */
var myAtoi = function(str) {
    let i = 0
    let output = '' 
    const intMax = 2**31 - 1
    const intMin = -intMax - 1
    while (i&lt;str.length) {
       const char = str[i]
       if (!char == " ") {
           if (char.toLowerCase() === char.toUpperCase()) {
               output = output + char
           } else if (output) {
               break
           }
           if (!output) return 0
       }
        i++
    }
    output = parseInt(output)
    if (output !== null &amp;&amp; !isNaN(output)) {
        if (output &gt; intMax) return  intMax
        if (output &lt; intMin) return intMin 
        return output
    }
    return 0
}


Can someone improve upon the same and give me suggestions on how I can improve my code?
Unfortunately there are several problems with your code. It only reason it works is because you are using parseInt which (as @wOxxOm shows) fulfills the original task requirements and thus ignores your broken attempts at cleaning the string.

!char == " "

This expression always returns true. Like in most programming languages negation (!) have a higher precedence than the comparison ==, so !char is resolved to false (unless char were an empty string, which in this case it never is) and false == " " resolves to true (to be honest I'm not sure why, I'd have to look it up. JavaScript's rules for non-strict comparison (==) are quite complex.) 

So it should be char != " " , or even better char !== " ". Always use strict comparison, unless you have a good reason not to.

char.toLowerCase() === char.toUpperCase()

This is a strange attempt to identify digits (and +/-). There are countless other characters that are not filtered by this.

32-bit signed integer range

You are missing the point of this rule. When the system is assumed not to be able to store integers outside the range, then that means a variable can't hold such a large/low number, so a comparison such as output &gt; intMax can never be true, since output can't physically contain a number larger than intMax.

Don't reuse variables

In output = parseInt(output) you change the type and the semantics of the variable output. This is confusing for the reader. And the name output for the original use of collecting the validated string wrong anyway.

Don't leave out semicolons

JavaScript's automatic semicolon insertion is nearly as complex as its non-strict comparison rules. By leaving out the semicolons you make the code more difficult to read because the reader then has an additional thing they need to consider.

Finally

The big problem here is you using parseInt. For one, as mentioned it's hiding the bugs in the code, and more importantly, you are missing the whole point of this exercise, which is to understand how string to number conversion actually works.