[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. ...
随机推荐
- Android活动状态和生存期
活动状态 1.运行状态(顶层的活动) 2.暂停状态(非顶层的,可见的活动) 3.停止状态(非顶层的,不可见的活动) 4.销毁状态(保证手机的内存充足) 活动的生存期 完整的生存期 onCreate活动 ...
- Linux下图解minicom安装
Linux下图解minicom安装 minicom是一个串口通信工具,就像Windows下的HyperTerminal.可用来与串口设备通信,如调试交换机和Modem等.它的Ubuntu软件包的名称就 ...
- vuex的mutations如何传多个传参?
1.不传参时的写法(官网例子): const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state) ...
- Swift学习笔记(6)--字典
1.定义 //1.基本定义 [key 1: value 1, key 2: value 2, key 3: value 3] var dict = ["name":"Xi ...
- Maintaining processor resources during architectural events
In one embodiment of the present invention, a method includes switching between a first address spac ...
- Struts2的token标签
“token标签的实现原理是在表单中增加一个隐藏域,每次加载该页面时,该隐藏域的值都不相同.而TokenInterceptor拦截器则拦截所有用户请求,如果两次请求时该token对应隐藏域的值相同(前 ...
- NStimer 被堵塞
我们在界面上滚动一个scrollview,那么我们会发如今停止滚动前,会发现NSTimer未被运行.就好像scrollView在滚动的时候将timer暂停了一样,在查看对应文档后发现,这事实上就是ru ...
- iOS项目开发实战——iOS网络编程获取网页Html源码
现在我们身处互联网的时代.不论什么一个软件或是App,都会或多或少与网络打交道,并不断发生数据交互.一个没有涉及网络编程的应用会显得比較low,这里我们将会開始使用Swift开发iOS应用,而且主要来 ...
- Android 简述touch事件中的MotionEvent
有关touchEvent的事件里都有一个 MotionEvent 參数,以下来简介一下它的属性的一些含义和使用的方法 通常单指操作时,一般例如以下: switch (event.getAction() ...
- android 图片特效处理之 光晕效果
这篇将讲到图片特效处理的图片光晕效果.跟前面一样是对像素点进行处理,本篇实现的思路可参见android图像处理系列之九--图片特效处理之二-模糊效果和android图像处理系列之十三--图片特效处理之 ...