Skip to content

Commit 282f31a

Browse files
authored
Merge pull request #15 from CodelyTV/js-bonus
[js][bonus] Add examples
2 parents fb0a489 + 3ed35df commit 282f31a

23 files changed

+18437
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.tmp/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env"],
3+
"plugins": ["@babel/plugin-proposal-class-properties"]
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
verbose: true,
4+
cacheDirectory: './.tmp/jestCache',
5+
transform: {'^.+\\.js$': 'babel-jest'},
6+
testMatch:[ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ],
7+
moduleFileExtensions: ['js']
8+
};

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

Lines changed: 6055 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "bonus",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"jest": {
12+
"moduleFileExtensions": [
13+
"js",
14+
"jsx"
15+
],
16+
"moduleDirectories": [
17+
"node_modules",
18+
"src"
19+
]
20+
},
21+
"devDependencies": {
22+
"@babel/core": "^7.11.6",
23+
"@babel/plugin-proposal-class-properties": "^7.10.4",
24+
"@babel/preset-env": "^7.11.5",
25+
"@jest/globals": "^26.4.2",
26+
"jest": "^26.4.2"
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export class DailyBonusPointsCalculator {
2+
PREMIUM_USER_POINTS_PER_DAY = 20;
3+
NORMAL_USER_POINTS_PER_DAY = 10;
4+
PREMIUM_USER_WEEKS_MULTIPLIER = 1.5;
5+
6+
calculate(consecutiveDays, isPremium = false) {
7+
if (isPremium) {
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;
16+
} else {
17+
return consecutiveDays * this.NORMAL_USER_POINTS_PER_DAY;
18+
}
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {DailyBonusPointsCalculator} from "../src/DailyBonusPointsCalculator";
2+
3+
describe('DailyBonusPointsCalculator should', () => {
4+
it('Calculate points for a normal user on its first day', () => {
5+
const calculator = new DailyBonusPointsCalculator();
6+
7+
expect(calculator.calculate(1)).toBe(10);
8+
});
9+
10+
it('Calculate points for a normal user on its second consecutive week', () => {
11+
const calculator = new DailyBonusPointsCalculator();
12+
13+
expect(calculator.calculate(14)).toBe(140);
14+
});
15+
16+
it('Calculate points for a premium user on its first day', () => {
17+
const calculator = new DailyBonusPointsCalculator();
18+
19+
expect(calculator.calculate(1, true)).toBe(20);
20+
});
21+
22+
it('Calculate points for a premium user on its second consecutive week', () => {
23+
const calculator = new DailyBonusPointsCalculator();
24+
25+
expect(calculator.calculate(14, true)).toBe(630);
26+
});
27+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.tmp/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env"],
3+
"plugins": ["@babel/plugin-proposal-class-properties"]
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
verbose: true,
4+
cacheDirectory: './.tmp/jestCache',
5+
transform: {'^.+\\.js$': 'babel-jest'},
6+
testMatch:[ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ],
7+
moduleFileExtensions: ['js']
8+
};

0 commit comments

Comments
 (0)