-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.js
More file actions
59 lines (51 loc) Β· 1.72 KB
/
clean.js
File metadata and controls
59 lines (51 loc) Β· 1.72 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
class Item {
constructor(value, description, user) {
this.value = value;
this.description = description;
this.user = user;
this.flag = null;
}
}
const budget = Object.freeze([
new Item(250, 'Sold old TV πΊ', 'jonas'),
new Item(-45, 'Groceries π₯', 'jonas'),
new Item(3500, 'Monthly salary π©βπ»', 'jonas'),
new Item(300, 'Freelancing π©βπ»', 'jonas'),
new Item(-1100, 'New iPhone π±', 'jonas'),
new Item(-20, 'Candy π', 'matilda'),
new Item(-125, 'Toys π', 'matilda'),
new Item(-1800, 'New Laptop π»', 'jonas'),
]);
const spendingLimits = Object.freeze({
jonas: 1500,
matilda: 100,
}); // Make the object immutable (just the first level of it, inside elements can still be modified)
const getLimit = (user, limits) => limits?.[user] ?? 0;
function addExpense(state, limits, value, description, user = 'jonas') {
user = user.toLowerCase();
if (value <= getLimit(user, limits))
return [...state, new Item(-value, description, user)];
return state;
}
function checkExpenses(state, limits) {
return state.map(el =>
el.value < -getLimit(el.user, limits) ? { ...el, flag: 'limit' } : el
);
}
function logBigExpenses(state, limit) {
console.log(
state
.filter(el => el.value <= -limit)
.map(el => el.description.slice(-2))
.join(' / ')
);
}
const budget1 = addExpense(budget, spendingLimits, 10, 'Pizza π');
// prettier-ignore
const budget2 = addExpense(budget1, spendingLimits, 100, 'Going to movies πΏ', 'Matilda');
const budget3 = addExpense(budget2, spendingLimits, 200, 'Stuff', 'Jay');
console.log(budget3);
const finalBudget = checkExpenses(budget3, spendingLimits);
console.log(finalBudget);
logBigExpenses(finalBudget, 1000);