-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeWarsStyleRankingSystem.js
43 lines (40 loc) · 1.02 KB
/
codeWarsStyleRankingSystem.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
/*
Problem URL
https://www.codewars.com/kata/51fda2d95d6efda45e00004e
*/
class User {
constructor() {
this.rank = -8;
this.progress = 0;
}
incProgress(completedRanked) {
if (completedRanked < -8 || completedRanked > 8 || completedRanked == 0) {
throw "OutOfRankRange";
}
let minusOfRankes = completedRanked - this.rank;
if (completedRanked > 0 && this.rank < 0) {
minusOfRankes--;
}
let addedProgress = 1;
if (minusOfRankes == 0) {
addedProgress = 3;
} else if (minusOfRankes >= 1) {
addedProgress = 10 * minusOfRankes * minusOfRankes;
}
this.calculateNewValues(addedProgress);
}
calculateNewValues(p) {
let totalProgress = this.progress + p;
let newAddedRank = Math.floor(totalProgress / 100);
this.progress = totalProgress % 100;
let isUnderZero = this.rank < 0;
this.rank += newAddedRank;
if (this.rank >= 0 && isUnderZero) {
this.rank++;
}
if (this.rank >= 8) {
this.rank = 8;
this.progress = 0;
}
}
}