Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit 7ce0f5b

Browse files
committed
feat: Completed Work in Fix Some Bad Code Second Section
* Transformed Some Impure Functions into Pure Ones
1 parent 4febe1b commit 7ce0f5b

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

09-modern-js/clean.js

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
const budget = [
24
{ value: 250, description: 'Sold old TV 📺', user: 'jonas' },
35
{ value: -45, description: 'Groceries 🥑', user: 'jonas' },
@@ -9,50 +11,46 @@ const budget = [
911
{ value: -1800, description: 'New Laptop 💻', user: 'jonas' },
1012
];
1113

12-
const spendingLimits = {
14+
const spendingLimits = Object.freeze({
1315
jonas: 1500,
1416
matilda: 100,
15-
};
17+
});
1618

17-
const getLimit = function (user)
19+
const getLimit = function (limits, user)
1820
{
1921
//return return = spendingLimits[user] ? spendingLimits[user] : 0;
20-
return spendingLimits?.[user] ?? 0;
22+
return limits?.[user] ?? 0;
2123
};
2224

23-
const addExpense = function (value, description, user = 'jonas')
25+
const addExpense = function (state, limits, value, description, user = 'jonas')
2426
{
25-
user = user.toLowerCase();
27+
const cleanUser = user.toLowerCase();
2628

27-
if (value > getLimit(user)) return;
29+
if (value > getLimit(spendingLimits, cleanUser)) return state;
2830

29-
budget.push({ value: -value, description, user });
31+
return [...state, { value: -value, description, user: cleanUser }];
3032
};
31-
addExpense(10, 'Pizza 🍕');
32-
addExpense(100, 'Going to movies 🍿', 'Matilda');
33-
addExpense(200, 'Stuff', 'Jay');
34-
console.log(budget);
33+
const newBudget1 = addExpense(budget, spendingLimits, 10, 'Pizza 🍕');
34+
const newBudget2 = addExpense(newBudget1, spendingLimits, 100, 'Going to movies 🍿', 'Matilda');
35+
const newBudget3 = addExpense(newBudget2, spendingLimits, 200, 'Stuff', 'Jay');
36+
console.log(newBudget3);
3537

36-
const check = function ()
38+
const checkExpenses = function (state)
3739
{
38-
budget.forEach(entry =>
39-
{
40-
if (entry.value >= -getLimit(entry.user)) return;
41-
42-
entry.flag = 'limit';
43-
});
40+
return state.map(entry =>
41+
entry.value < -getLimit(spendingLimits, entry.user) ? { ...entry, flag: 'limit' } : entry
42+
);
4443
};
45-
check();
44+
const finalBudget = checkExpenses(newBudget3, spendingLimits);
45+
console.log(finalBudget);
4646

47-
console.log(budget);
48-
49-
const logBigExpenses = function (bigLimit)
47+
const logBigExpenses = function (state, bigLimit)
5048
{
51-
const output = '';
52-
budget.forEach(entry =>
53-
{
54-
output += (entry.value <= -bigLimit) ? `${ entry.description.slice(-2) } / ` : ''; // Emojis are 2 chars
55-
});
56-
output = output.slice(0, -2); // Remove last '/ '
57-
console.log(output);
49+
let bigExpenses = state
50+
.filter(entry => entry.value <= -bigLimit)
51+
.map(entry => entry.description.slice(-2)) // Emojis are 2 chars)
52+
.join(' / ');
53+
54+
console.log(bigExpenses);
5855
};
56+
logBigExpenses(finalBudget, 500);

0 commit comments

Comments
 (0)