[TS] Parse a string to an integer
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的更多相关文章
- TS type different String / string
TS type different String / string String / string https://stackoverflow.com/questions/14727044/types ...
- Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?
Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么? Integer.valueof(String s)是将一个包装类是将一个实际 ...
- Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别
通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the ...
- String和包装类Integer\Double\Long\Float\Character 都是final类型
String和包装类Integer\Double\Long\Float\Character\Boolean 都是final类型 不可以改变
- The APK failed to install. Error:Could not parse error string.
问题一: The APK failed to install. Error:Could not parse error string. 今天拖拽自己的apk到模拟器上运行,报上述错误. 搜索解决方案. ...
- LINQ to Entities 不识别方法“System.Guid Parse(System.String)”,因此该方法无法转换为存储表达式。
LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型
- java5核心基础之泛型(3)-泛型作用于编译阶段-怎样将String对象传入Integer类型的泛型对象中?
泛型作用于编译阶段: 泛型是作用于编译阶段,在编译阶段控制类型,以确保在编写代码的时候仅仅能传入指定类型数据到泛型集合对象中去. 怎样验证呢,贴代码例如以下: package highBasic.ge ...
- [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 ...
- C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()、string到object 的区别
1.int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型); 2.int.Parse(string sParameter)是个构造函数,参数类型只支持string类型; 3. ...
随机推荐
- RMQ算法 以及UVA 11235 Frequent Values(RMQ)
RMQ算法 简单来说,RMQ算法是给定一组数据,求取区间[l,r]内的最大或最小值. 例如一组任意数据 5 6 8 1 3 11 45 78 59 66 4,求取区间(1,8) 内的最大值.数据量小 ...
- SQL 查找存在某内容的存储过程
--查找存在某表名的存储过程 SELECT distinct b.name from syscomments a,sysobjects b WHERE a.id=b.id and a.TEXT LIK ...
- Network Stack
Network Stack 目录 1 Overview 2 Code Layout 3 Anatomy of a Network Request (focused on HTTP) 3.1 URLRe ...
- OpenSUSE42.3 leap 软件源设置
一.OpenSUSE软件源介绍: 1.默认已经加入了官方的软件源,不过我们自己也可以根据需要添加很多非官方软件源,添加软件源时要注意: 非官方源可能包含一些试验中的不稳定的软件包 不同的软件源之 ...
- ES6学习笔记(五)函数的扩展
1.函数参数的默认值 1.1基本用法 ES6 之前,不能直接为函数的参数指定默认值,只能采用变通的方法. function log(x, y) { y = y || 'World'; console. ...
- strings---对象文件或二进制文件中查找可打印的字符串
strings命令在对象文件或二进制文件中查找可打印的字符串.字符串是4个或更多可打印字符的任意序列,以换行符或空字符结束. strings命令对识别随机对象文件很有用. 语法 strings [ - ...
- GCJ1C09C - Bribe the Prisoners
GCJ1C09C - Bribe the Prisoners Problem In a kingdom there are prison cells (numbered 1 to P) built t ...
- Objective-C(十九、通知-消息发送模式之中的一个)——iOS开发基础
结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结. 知识点一直在变.仅仅是作为參考.以苹果官方文档为准~ 十九.通知-消息 ...
- Flat UI简介
Flat UI简介 一.简介 Flat UI是基于Bootstrap之上进行二次开发的扁平化前端框架,他提供了动感.时尚的风格色调搭配,简洁.炫丽的功能组件,同时还提供了更为平滑的js交互动画,可以称 ...
- Vue项目自动转换 px 为 rem,高保真还原设计图
技术栈 vue-cli:使用脚手架工具创建项目. postcss-pxtorem:转换px为rem的插件. 自动设置根节点html的font-size 因为rem单位是相对于根节点的字体大小的,所以通 ...