-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path171_Excel_Sheet_Column_Number.js
More file actions
39 lines (34 loc) · 1.01 KB
/
171_Excel_Sheet_Column_Number.js
File metadata and controls
39 lines (34 loc) · 1.01 KB
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
/*
171. Excel Sheet Column Number
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
*/
const expect = require('expect');
describe('171 Excel Sheet Column Number', () => {
it('returns column number', () => {
//arrange
const input = "AT";
const expected = 46;
//act
const actual = titleToNumber(input);
//assert
expect(actual).toBe(expected);
});
it('returns column number', () => {
//arrange
const input = "TGED";
const expected = 356386;
//act
const actual = titleToNumber(input);
//assert
expect(actual).toBe(expected);
});
});
const titleToNumber_1 = string => [...string]
.reverse()
.map((value, index) => (value.charCodeAt(0) - 64) * (26 ** index))
.reduce((acc, cur) => acc + cur, 0);
// more concise but not straight forward
const titleToNumber_2 = string => [...string]
.reverse()
.reduce((acc, cur, index) => acc + (cur.charCodeAt(0) - 64) * (26 ** index), 0);