-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringToInt.js
50 lines (43 loc) · 1.28 KB
/
stringToInt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
In nearly every language, there exist various utility functions to convert a string to an integer. Eg. in JavaScript, JSON.parse("4") returns the integer 4.
We want you to rewrite this utility function from scratch. Needless to say, you are not allowed to use any similar utility method that directly does this conversion for you.
*/
/*
* Complete the 'stringToInt' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING num as parameter.
*/
const stringToIntMap = {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
};
function multiplyByTenNTimes(num, n) {
let product = num;
while(n>0) {
product*=10;
--n;
}
return product;
}
function stringToInt(num) {
const numArr = num.split("");
return numArr.map((numStr, idx) => {
const multiplierAmount = (numArr.length - 1) - idx;
const numInt = stringToIntMap[numStr];
return multiplyByTenNTimes(numInt, multiplierAmount);
})
.reduce((prev, curr) => prev + curr);
};
// Examples
console.log('stringToInt("513")', stringToInt("513")); //513
console.log('stringToInt("503")', stringToInt("503")); //503
console.log('stringToInt("0")', stringToInt("0")); // 0