Skip to content

Commit b09649c

Browse files
committed
[js][bonus-01_base] Add consecutive weeks multiplier for premium user daily bonus
1 parent 870dace commit b09649c

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

examples/js/js-bonus-01_base/package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "student_grades",
2+
"name": "bonus",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
@@ -24,9 +24,5 @@
2424
"@babel/preset-env": "^7.11.5",
2525
"@jest/globals": "^26.4.2",
2626
"jest": "^26.4.2"
27-
},
28-
"dependencies": {
29-
"sync-mysql": "^3.0.1",
30-
"sync-sql": "^1.0.2"
3127
}
3228
}

examples/js/js-bonus-01_base/src/DailyBonusPointsCalculator.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
export class DailyBonusPointsCalculator {
22
PREMIUM_USER_POINTS_PER_DAY = 20;
33
NORMAL_USER_POINTS_PER_DAY = 10;
4+
PREMIUM_USER_WEEKS_MULTIPLIER = 1.5;
45

5-
calculate(consecutiveDays, isPremium) {
6+
calculate(consecutiveDays, isPremium = false) {
67
if (isPremium) {
7-
return consecutiveDays * this.PREMIUM_USER_POINTS_PER_DAY;
8+
const consecutiveWeeks = Math.floor(consecutiveDays / 7);
9+
10+
let consecutiveWeeksMultiplier = 1;
11+
if (consecutiveWeeks > 0) {
12+
consecutiveWeeksMultiplier = Math.pow(this.PREMIUM_USER_WEEKS_MULTIPLIER, consecutiveWeeks);
13+
}
14+
15+
return consecutiveDays * this.PREMIUM_USER_POINTS_PER_DAY * consecutiveWeeksMultiplier;
816
} else {
917
return consecutiveDays * this.NORMAL_USER_POINTS_PER_DAY;
1018
}

examples/js/js-bonus-01_base/tests/DailyBonusPointsCalculator.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ describe('DailyBonusPointsCalculator should', () => {
44
it('Calculate points for a normal user on its first day', () => {
55
const calculator = new DailyBonusPointsCalculator();
66

7-
expect(calculator.calculate(1, false)).toBe(10);
7+
expect(calculator.calculate(1)).toBe(10);
88
});
99

1010
it('Calculate points for a normal user on its second consecutive week', () => {
1111
const calculator = new DailyBonusPointsCalculator();
1212

13-
expect(calculator.calculate(14, false)).toBe(140);
13+
expect(calculator.calculate(14)).toBe(140);
1414
});
1515

1616
it('Calculate points for a premium user on its first day', () => {
@@ -22,6 +22,6 @@ describe('DailyBonusPointsCalculator should', () => {
2222
it('Calculate points for a premium user on its second consecutive week', () => {
2323
const calculator = new DailyBonusPointsCalculator();
2424

25-
expect(calculator.calculate(14, true)).toBe(280);
25+
expect(calculator.calculate(14, true)).toBe(630);
2626
});
2727
});

0 commit comments

Comments
 (0)