Skip to content

Commit 614e168

Browse files
committed
[EX-6.5/st-compl] buying-game
Writing/condition to check possibility/buying game, by "logical" opr's. Worth noting: - a few solutions (Jest tests). FS-dev: B-3 / JS basic
1 parent 0e6b6ca commit 614e168

File tree

4 files changed

+4523
-0
lines changed

4 files changed

+4523
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Пользователь хочет приобрести игру в магазине
2+
// Он может это сделать только если:
3+
// - Eго баланс больше 1000 (balance) или число бонусов больше 100 (bonusBalance)
4+
// - Он не забанен (isBanned)
5+
// - Игра не куплена (isExist)
6+
// - Игра в продаже (isSelling)
7+
// Напишите условие для покупки и выведите в консоль результат
8+
9+
const userBalance = 1500;
10+
const userBonusBalance = 99;
11+
const isUserBanned = false;
12+
const isGamePurchased = false;
13+
const isGameSelling = true;
14+
15+
const result =
16+
(userBalance > 1000 || userBonusBalance > 100) &&
17+
!isUserBanned &&
18+
!isGamePurchased &&
19+
isGameSelling;
20+
21+
console.log(`Пользователь может купить игру: ${result ? 'Да!' : 'Нет..'}`); // Пользователь может купить игру: Да!
22+
23+
// ? альтернативное решение (несколько переменных - нагляднее формирование результата)
24+
// const canAfford = userBalance > 1000 || userBonusBalance > 100;
25+
// const isAvailable = !isUserBanned && !isGamePurchased && isGameSelling;
26+
// const result = canAfford && isAvailable;
27+
28+
// ? альтернативное решение (через функцию и объекты, с авто-тестами)
29+
function canUserBuyGame(user, game) {
30+
const { balance, bonusBalance, isBanned, hasPurchased } = user;
31+
const { isSelling } = game;
32+
33+
return (
34+
(balance > 1000 || bonusBalance > 100) &&
35+
!isBanned &&
36+
!hasPurchased &&
37+
isSelling
38+
);
39+
}
40+
41+
module.exports = canUserBuyGame;
42+
43+
const user = {
44+
balance: 1500,
45+
bonusBalance: 99,
46+
isBanned: false,
47+
hasPurchased: false,
48+
};
49+
const game = { isSelling: true };
50+
51+
const canBuy = canUserBuyGame(user, game);
52+
console.log(`Пользователь может купить игру: ${canBuy ? 'Да!' : 'Нет..'}`); // Пользователь может купить игру: Да!
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const canUserBuyGame = require('./index');
2+
3+
describe('Game Purchase Logic', () => {
4+
it('should return true if the user can buy the game', () => {
5+
const user = {
6+
balance: 1500,
7+
bonusBalance: 99,
8+
isBanned: false,
9+
hasPurchased: false,
10+
};
11+
const game = {
12+
isSelling: true,
13+
};
14+
expect(canUserBuyGame(user, game)).toBe(true);
15+
});
16+
17+
it('should return false if the user is banned', () => {
18+
const user = {
19+
balance: 1500,
20+
bonusBalance: 99,
21+
isBanned: true,
22+
hasPurchased: false,
23+
};
24+
const game = {
25+
isSelling: true,
26+
};
27+
expect(canUserBuyGame(user, game)).toBe(false);
28+
});
29+
30+
it('should return false if the game is already purchased', () => {
31+
const user = {
32+
balance: 1500,
33+
bonusBalance: 99,
34+
isBanned: false,
35+
hasPurchased: true,
36+
};
37+
const game = {
38+
isSelling: true,
39+
};
40+
expect(canUserBuyGame(user, game)).toBe(false);
41+
});
42+
43+
it('should return false if the game is not for sale', () => {
44+
const user = {
45+
balance: 1500,
46+
bonusBalance: 99,
47+
isBanned: false,
48+
hasPurchased: false,
49+
};
50+
const game = {
51+
isSelling: false,
52+
};
53+
expect(canUserBuyGame(user, game)).toBe(false);
54+
});
55+
56+
it('should return true if the user has enough bonus balance', () => {
57+
const user = {
58+
balance: 500,
59+
bonusBalance: 150,
60+
isBanned: false,
61+
hasPurchased: false,
62+
};
63+
const game = { isSelling: true };
64+
expect(canUserBuyGame(user, game)).toBe(true);
65+
});
66+
});

0 commit comments

Comments
 (0)