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. Redis封装值ZSet

    /// <summary> /// Sorted Sets是将 Set 中的元素增加了一个权重参数 score,使得集合中的元素能够按 score 进行有序排列 /// 1.带有权重的元素 ...

  2. 关于Javascript的forEach 和 map

    本篇博客转载自 https://blog.fundebug.com/2018/02/05/map_vs_foreach/ 如果你已经有使用JavaScript的经验,你可能已经知道这两个看似相同的方法 ...

  3. orm 通用方法——DeleteModel 主键删除

    定义代码: /** * 描述:删除对象 * 作者:Tianqi * 日期:2014-09-17 * param:model 对象实例,包含主键 * return:int 受影响行数 * return: ...

  4. 记录一下sql两个表关联的查询使用方法

    SELECT * FROM t_yymp_user_info where user_id = (select b.user_id from t_yymp_auth_role as a,t_yymp_a ...

  5. VirtualBox中Linux虚拟机与主机共享文件夹

    VirtualBox中Linux虚拟机与主机共享文件夹 一.Linux虚拟机安装增强功能 二.点击虚拟机 设置-->选择 共享文件夹-->点击右侧的带加号的文件夹图标,执行下面的操作1. ...

  6. CentOS桥接网卡配置

    网桥方式配置步骤 1.增加网桥设备br0 vi /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE=br0 ONBOOT=yes TYPE=Bridge B ...

  7. TCP简单说|(上)

    本文在Creative Commons许可证下发布 TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过程,但对于学习的过程却能让人 ...

  8. 【Uva 10723】Cyborg Genes

    [Link]: [Description] 给你两个串s1,s2; 让你生成一个串S; 使得s1和s2都是S的子列; 要求S最短; 求S的不同方案个数; [Solution] 设两个串的长度分别为n1 ...

  9. window 搭建python环境

    Unofficial Windows Binaries for Python Extension Packages 其中包含大量Windows下的python的module 包含大但不仅限于pip: ...

  10. Unix/Linux环境C编程新手教程(37) shell经常使用命令演练

     cat命令 cat命令能够用来查看文件内容. cat [參数] 文件名称. grep-指定文件里搜索指定字符内容. Linux的文件夹或文件. -path '字串' 查找路径名匹配所给字串的全部 ...