-
Notifications
You must be signed in to change notification settings - Fork 16.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f1ffbc
commit d817e0e
Showing
5 changed files
with
217 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,86 @@ | ||
const shoppingCart = [ | ||
{ product: 'bread', quantity: 6 }, | ||
{ product: 'pizza', quantity: 2 }, | ||
{ product: 'milk', quantity: 4 }, | ||
{ product: 'water', quantity: 10 }, | ||
]; | ||
|
||
const allowedProducts = { | ||
lisbon: 5, | ||
others: 7, | ||
}; | ||
'strict mode'; | ||
|
||
const budget = Object.freeze([ | ||
{ value: 250, description: 'Sold old TV 📺', user: 'jonas' }, | ||
{ value: -45, description: 'Groceries 🥑', user: 'jonas' }, | ||
{ value: 3500, description: 'Monthly salary 👩💻', user: 'jonas' }, | ||
{ value: 300, description: 'Freelancing 👩💻', user: 'jonas' }, | ||
{ value: -1100, description: 'New iPhone 📱', user: 'jonas' }, | ||
{ value: -20, description: 'Candy 🍭', user: 'matilda' }, | ||
{ value: -125, description: 'Toys 🚂', user: 'matilda' }, | ||
{ value: -1800, description: 'New Laptop 💻', user: 'jonas' }, | ||
]); | ||
|
||
const checkCorrectAllowedProducts = function (cart, numAllowed, city) { | ||
if (!cart.length) return []; | ||
const spendingLimits = Object.freeze({ | ||
jonas: 1500, | ||
matilda: 100, | ||
}); | ||
// spendingLimits.jay = 200; | ||
|
||
// const allowed = numAllowed[city] > 0 ? numAllowed[city] : numAllowed.others; | ||
const allowed = numAllowed?.[city] ?? allowedProducts.others; | ||
// const limit = spendingLimits[user] ? spendingLimits[user] : 0; | ||
const getLimit = (limits, user) => limits?.[user] ?? 0; | ||
|
||
const newCart = cart.map(item => { | ||
const { product, quantity } = item; | ||
return { | ||
product, | ||
quantity: quantity > allowed ? allowed : quantity, | ||
}; | ||
}); | ||
// Pure function :D | ||
const addExpense = function ( | ||
state, | ||
limits, | ||
value, | ||
description, | ||
user = 'jonas' | ||
) { | ||
const cleanUser = user.toLowerCase(); | ||
|
||
return newCart; | ||
return value <= getLimit(limits, cleanUser) | ||
? [...state, { value: -value, description, user: cleanUser }] | ||
: state; | ||
}; | ||
const allowedShoppingCart = checkCorrectAllowedProducts( | ||
shoppingCart, | ||
allowedProducts, | ||
'lisbon' | ||
// 'faro' | ||
|
||
const newBudget1 = addExpense(budget, spendingLimits, 10, 'Pizza 🍕'); | ||
const newBudget2 = addExpense( | ||
newBudget1, | ||
spendingLimits, | ||
100, | ||
'Going to movies 🍿', | ||
'Matilda' | ||
); | ||
console.log(allowedShoppingCart); | ||
const newBudget3 = addExpense(newBudget2, spendingLimits, 200, 'Stuff', 'Jay'); | ||
|
||
// const checkExpenses2 = function (state, limits) { | ||
// return state.map(entry => { | ||
// return entry.value < -getLimit(limits, entry.user) | ||
// ? { ...entry, flag: 'limit' } | ||
// : entry; | ||
// }); | ||
// // for (const entry of newBudget3) | ||
// // if (entry.value < -getLimit(limits, entry.user)) entry.flag = 'limit'; | ||
// }; | ||
|
||
const checkExpenses = (state, limits) => | ||
state.map(entry => | ||
entry.value < -getLimit(limits, entry.user) | ||
? { ...entry, flag: 'limit' } | ||
: entry | ||
); | ||
|
||
const finalBudget = checkExpenses(newBudget3, spendingLimits); | ||
console.log(finalBudget); | ||
|
||
// Impure | ||
const logBigExpenses = function (state, bigLimit) { | ||
const bigExpenses = state | ||
.filter(entry => entry.value <= -bigLimit) | ||
.map(entry => entry.description.slice(-2)) | ||
.join(' / '); | ||
// .reduce((str, cur) => `${str} / ${cur.description.slice(-2)}`, ''); | ||
|
||
const createOrderDescription = function (cart) { | ||
const [{ product: p, quantity: q }] = cart; | ||
console.log(bigExpenses); | ||
|
||
return `Order with ${q} ${p}${cart.length > 1 ? ', etc...' : '.'}`; | ||
// let output = ''; | ||
// for (const entry of budget) | ||
// output += | ||
// entry.value <= -bigLimit ? `${entry.description.slice(-2)} / ` : ''; | ||
// output = output.slice(0, -2); // Remove last '/ ' | ||
// console.log(output); | ||
}; | ||
const orderDescription = createOrderDescription(allowedShoppingCart); | ||
|
||
console.log(orderDescription); | ||
logBigExpenses(finalBudget, 500); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,64 @@ | ||
var sc = [ | ||
{ product: 'bread', quantity: 6 }, | ||
{ product: 'pizza', quantity: 2 }, | ||
{ product: 'milk', quantity: 4 }, | ||
{ product: 'water', quantity: 10 }, | ||
var budget = [ | ||
{ value: 250, description: 'Sold old TV 📺', user: 'jonas' }, | ||
{ value: -45, description: 'Groceries 🥑', user: 'jonas' }, | ||
{ value: 3500, description: 'Monthly salary 👩💻', user: 'jonas' }, | ||
{ value: 300, description: 'Freelancing 👩💻', user: 'jonas' }, | ||
{ value: -1100, description: 'New iPhone 📱', user: 'jonas' }, | ||
{ value: -20, description: 'Candy 🍭', user: 'matilda' }, | ||
{ value: -125, description: 'Toys 🚂', user: 'matilda' }, | ||
{ value: -1800, description: 'New Laptop 💻', user: 'jonas' }, | ||
]; | ||
|
||
var allow = { | ||
lisbon: 5, | ||
others: 7, | ||
var limits = { | ||
jonas: 1500, | ||
matilda: 100, | ||
}; | ||
|
||
var description = ''; | ||
var add = function (value, description, user) { | ||
if (!user) user = 'jonas'; | ||
user = user.toLowerCase(); | ||
|
||
var check = function (city) { | ||
if (sc.length > 0) { | ||
var allowed; | ||
if (city == 'lisbon') { | ||
allowed = allow.lisbon; | ||
var lim; | ||
if (limits[user]) { | ||
lim = limits[user]; | ||
} else { | ||
lim = 0; | ||
} | ||
|
||
if (value <= lim) { | ||
budget.push({ value: -value, description: description, user: user }); | ||
} | ||
}; | ||
add(10, 'Pizza 🍕'); | ||
add(100, 'Going to movies 🍿', 'Matilda'); | ||
add(200, 'Stuff', 'Jay'); | ||
console.log(budget); | ||
|
||
var check = function () { | ||
for (var el of budget) { | ||
var lim; | ||
if (limits[el.user]) { | ||
lim = limits[el.user]; | ||
} else { | ||
allowed = allow.others; | ||
lim = 0; | ||
} | ||
|
||
for (item of sc) { | ||
if (item.quantity > allowed) item.quantity = allowed; | ||
if (el.value < -lim) { | ||
el.flag = 'limit'; | ||
} | ||
} | ||
}; | ||
check('lisbon'); | ||
console.log(sc); | ||
check(); | ||
|
||
var createDescription = function () { | ||
var first = sc[0]; | ||
var p = first.product; | ||
var q = first.quantity; | ||
console.log(budget); | ||
|
||
if (sc.length > 1) { | ||
description = 'Order with ' + q + ' ' + p + ', etc...'; | ||
} else { | ||
description = 'Order with ' + q + ' ' + p + '.'; | ||
var bigExpenses = function (limit) { | ||
var output = ''; | ||
for (var el of budget) { | ||
if (el.value <= -limit) { | ||
output += el.description.slice(-2) + ' / '; // Emojis are 2 chars | ||
} | ||
} | ||
output = output.slice(0, -2); // Remove last '/ ' | ||
console.log(output); | ||
}; | ||
createDescription(); | ||
|
||
console.log(description); |