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.

Writing a function whichi convert string to number, to do that

1. Convert each "string" char to ASCII code by using

str.charCodeAt(index)

2. Each round, we should increase the acc value by *10

function atoi (str: string): number {
const zeroCode = '0'.charCodeAt(0);
console.log("zeroCode", zeroCode); let sub = 1;
if(str[0] === '-') {
sub = -1;
str = str.substring(1);
console.log("sub string", str);
} return sub * str.split('')
.reduce((acc, curr) => {
acc = acc * 10 + (curr.charCodeAt(0) - zeroCode)
return acc;
}, 0)
} console.log(atoi("123")); //123
console.log(atoi("-123")); //-123
import { atoi } from './atoi';

test('basic', () => {
expect(atoi('123')).toBe(123);
expect(atoi('-1123')).toBe(-1123);
});

[TS] Parse a string to an integer的更多相关文章

  1. TS type different String / string

    TS type different String / string String / string https://stackoverflow.com/questions/14727044/types ...

  2. Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?

    Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么? Integer.valueof(String s)是将一个包装类是将一个实际 ...

  3. Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别

    通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the ...

  4. String和包装类Integer\Double\Long\Float\Character 都是final类型

    String和包装类Integer\Double\Long\Float\Character\Boolean 都是final类型 不可以改变

  5. The APK failed to install. Error:Could not parse error string.

    问题一: The APK failed to install. Error:Could not parse error string. 今天拖拽自己的apk到模拟器上运行,报上述错误. 搜索解决方案. ...

  6. LINQ to Entities 不识别方法“System.Guid Parse(System.String)”,因此该方法无法转换为存储表达式。

    LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型

  7. java5核心基础之泛型(3)-泛型作用于编译阶段-怎样将String对象传入Integer类型的泛型对象中?

    泛型作用于编译阶段: 泛型是作用于编译阶段,在编译阶段控制类型,以确保在编写代码的时候仅仅能传入指定类型数据到泛型集合对象中去. 怎样验证呢,贴代码例如以下: package highBasic.ge ...

  8. [string]Roman to Integer,Integer to Roman

    一.Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within ...

  9. C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()、string到object 的区别

    1.int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型); 2.int.Parse(string sParameter)是个构造函数,参数类型只支持string类型; 3. ...

随机推荐

  1. beego实现web api接口

    1)程序代码: /** * 类似beego版物联网首页产品数据的调用 */import (    "github.com/astaxie/beego"    "githu ...

  2. leetcode 10 Regular Expression Matching(简单正则表达式匹配)

    最近代码写的少了,而leetcode一直想做一个python,c/c++解题报告的专题,c/c++一直是我非常喜欢的,c语言编程练习的重要性体现在linux内核编程以及一些大公司算法上机的要求,pyt ...

  3. msiexec

    msiexec: runCmd = new String[]{ "msiexec", "/i", exeName, "/quiet", &q ...

  4. 实现人脸识别性别之路---matplotlib

    Np.linspace(start,stop,num,endpoint,dtype)函数 1.参数:范围值,在范围值中取到的数值总数.是否包含范围值.类型 2.返回值:返回一维数据 3.在指定的范围内 ...

  5. BZOJ3294: [Cqoi2011]放棋子(计数Dp,组合数学)

    题目链接 解题思路: 发现一个性质,如果考虑一个合法的方案可以将行和列都压到一起,也就是说,在占用行数和列数一定的情况下,行列互换是不会影响答案的,那么考虑使用如下方程: $f[i][j][k]$为占 ...

  6. 解决spring-boot启动中碰到的问题:Cannot determine embedded database driver class for database type NONE(转)

    问题 如下: 2017-07-16 08:50:57.436  INFO 13524 --- [           main] c.p.p.web.PointshopWebApplication   ...

  7. C# 对Excel操作时,单元格值的读取

    一.Range中Value与Value2的区别 当range("A1:B10")设置为 Currency (货币)和 Date (日期.日期时间)数据类型时,range2将返回对应 ...

  8. PHP开发常规安全问题总结

    文章来源:PHP开发学习门户 地址:http://www.phpthinking.com/archives/575 php给了开发人员极大的灵活性,可是这也为安全问题带来了潜在的隐患,最近须要总结一下 ...

  9. BZOJ 4582 贪心

    思路: 显然是个贪心 排个序 然后扫几遍就好了 (重叠的区间不能取) //By SiriusRen #include <cstdio> #include <algorithm> ...

  10. POJ 2433 枚举

    题意: 思路: 每回枚举去哪个山包 枚举的姿势很重要 //By SiriusRen #include <cstdio> #include <algorithm> using n ...