-
Notifications
You must be signed in to change notification settings - Fork 0
/
centuryFromYear.js
32 lines (29 loc) · 1004 Bytes
/
centuryFromYear.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
// https://www.codewars.com/kata/5a3fe3dde1ce0e8ed6000097/train/javascript
function century(year) {
let yearString = year.toString();
if (year < 101) {
return 1;
} else if (year < 1001) {
return Number(yearString.toString().slice(0, 1));
} else if (year <= 10001) {
let decimalYear =
yearString.substring(0, 2) +
"." +
yearString.substring(2, yearString.length);
return Number(Math.ceil(Number(decimalYear)).toString().slice(0, 2));
} else if (year <= 100001) {
let decimalYear =
yearString.substring(0, 3) +
"." +
yearString.substring(3, yearString.length);
return Number(Math.ceil(Number(decimalYear)).toString().slice(0, 4));
} else if (year <= 1000001) {
let decimalYear =
yearString.substring(0, 4) +
"." +
yearString.substring(4, yearString.length);
return Number(Math.ceil(Number(decimalYear)).toString().slice(0, 4));
}
}
// simpler solution..
const century = (year) => Math.ceil(year / 100);