 Unlock This Lesson
1×
Become a member
to unlock all features Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

# Parse a string to an integer TypeScript
^2.1.4

A common interview question is to write  a function that converts  a  string into an integer e.g. `"123"` => `123`.  This  function  is commonly  called  `atoi` because  we  are  converting  an  ASCII  string  into  an  integer.

In this lesson we cover the proper way to do this in JavaScript which is `parseInt` along with implementing it using basic ascii math. ### Code ### You must be a Member to view code

Access all courses and lessons, track your progress, gain confidence and expertise.

Become a Member
and unlock code for this lesson ### Discuss ### Transcript

A common interview question is to write a function that takes a string, parses it, and returns its integer value. For example, the string 123 to its integer value 123. We start by creating our atoi function that takes a string and returns a number.

JavaScript has a function called parseInt that can do this integer parsing for us. Let's run through a few example of this built-in functions behavior. For simple ints, we get an int. For ints with a negative sign, we get a negative int.

When it encounters a character that is not numeral, it ignores it and all succeeding characters. It still returns a integer value parsed up to that point so far -123 extra, we'll still get -123. You can use this fact to easily ignore decimal positions as demonstrated in this example.

Finally, if the string does not start with the decimal numeral, it returns NaN. That is, "Not a Number." If you go ahead and run the example, the output matches our expectations. Note that you can customize the behavior of parseInt quite easily by wrapping it in a function.

For example, parseInt silently ignoring invalid trading characters can be considered a behavior you want to change. You can easily implement a more inaudible purpose function with a simple regex test that only allows strings containing things that we support.

For example, a waiting sign followed by any number of decimal digits. In all other cases, it will return NaN. Now, all imperfect inputs to a function will return NaN, and we getting verify by running our example.

With practical JavaScript training out of the way, when this question is commonly asked, the interviewer might specify that you are not allowed to use parseInt, or any other built-in number parsing functionality. They essentially want to see if you can write a function that is similar to how parseInt would be implemented internally.

First, we note down the corrective point for zero. This will allow us to convert each decimal digit from a string to its number value. The next thing we need to do is to parse out the sign. We can store this in the sign multiplier.

Next, we create an accumulator for the integer result of parsing the digits. We go through the string left to right. At each point, we update the accumulator as the sum of the current accumulator multiplied by 10, and the number value of the current character which we get from its distance from the zero code.

Finally, we return the accumulator multiplied by the sign. If you run through the example with binary inputs, you see that it works as expected. This answer shows your understanding of basic math, like the fact that digits represent powers of 10. This solution also shows an understanding of the fact that string digits represent code points that can be inaudible to get their integer representation.

The interviewer might additionally ask you about things that can go wrong in this implementation. A good one worth mentioning is that we are assuming that the string only has valid digits. They might then ask you to fix that issue, and you can simply check the result of this difference to be in the range zero to nine.