Skip to content

Commit d817e0e

Browse files
v2.1
1 parent 9f1ffbc commit d817e0e

File tree

5 files changed

+217
-65
lines changed

5 files changed

+217
-65
lines changed

09-Data-Structures-Operators/final/script.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ const restaurant = {
4848
},
4949
};
5050

51+
/*
52+
///////////////////////////////////////
53+
// String Methods Practice
54+
55+
const flights =
56+
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';
57+
58+
// 🔴 Delayed Departure from FAO to TXL (11h25)
59+
// Arrival from BRU to FAO (11h45)
60+
// 🔴 Delayed Arrival from HEL to FAO (12h05)
61+
// Departure from FAO to LIS (12h30)
62+
63+
const getCode = str => str.slice(0, 3).toUpperCase();
64+
65+
for (const flight of flights.split('+')) {
66+
const [type, from, to, time] = flight.split(';');
67+
const output = `${type.startsWith('_Delayed') ? '🔴' : ''}${type.replaceAll(
68+
'_',
69+
' '
70+
)} ${getCode(from)} ${getCode(to)} (${time.replace(':', 'h')})`.padStart(36);
71+
console.log(output);
72+
}
73+
*/
74+
5175
///////////////////////////////////////
5276
// Coding Challenge #4
5377

09-Data-Structures-Operators/starter/script.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict';
22

3+
// Data needed for a later exercise
4+
const flights =
5+
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';
6+
7+
// Data needed for first part of the section
38
const restaurant = {
49
name: 'Classico Italiano',
510
location: 'Via Angelo Tavanti 23, Firenze, Italy',

11-Arrays-Bankist/final/script.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ console.log(movements);
609609
const arr = [1, 2, 3, 4, 5, 6, 7];
610610
console.log(new Array(1, 2, 3, 4, 5, 6, 7));
611611
612-
// Emprty arrays + fill method
612+
// Empty arrays + fill method
613613
const x = new Array(7);
614614
console.log(x);
615615
// console.log(x.map(() => 5));
@@ -636,6 +636,68 @@ labelBalance.addEventListener('click', function () {
636636
637637
const movementsUI2 = [...document.querySelectorAll('.movements__value')];
638638
});
639+
640+
641+
///////////////////////////////////////
642+
// Array Methods Practice
643+
644+
// 1.
645+
const bankDepositSum = accounts
646+
.flatMap(acc => acc.movements)
647+
.filter(mov => mov > 0)
648+
.reduce((sum, cur) => sum + cur, 0);
649+
650+
console.log(bankDepositSum);
651+
652+
// 2.
653+
// const numDeposits1000 = accounts
654+
// .flatMap(acc => acc.movements)
655+
// .filter(mov => mov >= 1000).length;
656+
657+
const numDeposits1000 = accounts
658+
.flatMap(acc => acc.movements)
659+
.reduce((count, cur) => (cur >= 1000 ? ++count : count), 0);
660+
661+
console.log(numDeposits1000);
662+
663+
// Prefixed ++ operator
664+
let a = 10;
665+
console.log(++a);
666+
console.log(a);
667+
668+
// 3.
669+
const { deposits, withdrawals } = accounts
670+
.flatMap(acc => acc.movements)
671+
.reduce(
672+
(sums, cur) => {
673+
// cur > 0 ? (sums.deposits += cur) : (sums.withdrawals += cur);
674+
sums[cur > 0 ? 'deposits' : 'withdrawals'] += cur;
675+
return sums;
676+
},
677+
{ deposits: 0, withdrawals: 0 }
678+
);
679+
680+
console.log(deposits, withdrawals);
681+
682+
// 4.
683+
// this is a nice title -> This Is a Nice Title
684+
const convertTitleCase = function (title) {
685+
const capitalize = str => str[0].toUpperCase() + str.slice(1);
686+
687+
const exceptions = ['a', 'an', 'and', 'the', 'but', 'or', 'on', 'in', 'with'];
688+
689+
const titleCase = title
690+
.toLowerCase()
691+
.split(' ')
692+
.map(word => (exceptions.includes(word) ? word : capitalize(word)))
693+
.join(' ');
694+
695+
return capitalize(titleCase);
696+
};
697+
698+
console.log(convertTitleCase('this is a nice title'));
699+
console.log(convertTitleCase('this is a LONG title but not too long'));
700+
console.log(convertTitleCase('and here is another title with an EXAMPLE'));
639701
*/
640702

641703
///////////////////////////////////////
Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,86 @@
1-
const shoppingCart = [
2-
{ product: 'bread', quantity: 6 },
3-
{ product: 'pizza', quantity: 2 },
4-
{ product: 'milk', quantity: 4 },
5-
{ product: 'water', quantity: 10 },
6-
];
7-
8-
const allowedProducts = {
9-
lisbon: 5,
10-
others: 7,
11-
};
1+
'strict mode';
2+
3+
const budget = Object.freeze([
4+
{ value: 250, description: 'Sold old TV 📺', user: 'jonas' },
5+
{ value: -45, description: 'Groceries 🥑', user: 'jonas' },
6+
{ value: 3500, description: 'Monthly salary 👩‍💻', user: 'jonas' },
7+
{ value: 300, description: 'Freelancing 👩‍💻', user: 'jonas' },
8+
{ value: -1100, description: 'New iPhone 📱', user: 'jonas' },
9+
{ value: -20, description: 'Candy 🍭', user: 'matilda' },
10+
{ value: -125, description: 'Toys 🚂', user: 'matilda' },
11+
{ value: -1800, description: 'New Laptop 💻', user: 'jonas' },
12+
]);
1213

13-
const checkCorrectAllowedProducts = function (cart, numAllowed, city) {
14-
if (!cart.length) return [];
14+
const spendingLimits = Object.freeze({
15+
jonas: 1500,
16+
matilda: 100,
17+
});
18+
// spendingLimits.jay = 200;
1519

16-
// const allowed = numAllowed[city] > 0 ? numAllowed[city] : numAllowed.others;
17-
const allowed = numAllowed?.[city] ?? allowedProducts.others;
20+
// const limit = spendingLimits[user] ? spendingLimits[user] : 0;
21+
const getLimit = (limits, user) => limits?.[user] ?? 0;
1822

19-
const newCart = cart.map(item => {
20-
const { product, quantity } = item;
21-
return {
22-
product,
23-
quantity: quantity > allowed ? allowed : quantity,
24-
};
25-
});
23+
// Pure function :D
24+
const addExpense = function (
25+
state,
26+
limits,
27+
value,
28+
description,
29+
user = 'jonas'
30+
) {
31+
const cleanUser = user.toLowerCase();
2632

27-
return newCart;
33+
return value <= getLimit(limits, cleanUser)
34+
? [...state, { value: -value, description, user: cleanUser }]
35+
: state;
2836
};
29-
const allowedShoppingCart = checkCorrectAllowedProducts(
30-
shoppingCart,
31-
allowedProducts,
32-
'lisbon'
33-
// 'faro'
37+
38+
const newBudget1 = addExpense(budget, spendingLimits, 10, 'Pizza 🍕');
39+
const newBudget2 = addExpense(
40+
newBudget1,
41+
spendingLimits,
42+
100,
43+
'Going to movies 🍿',
44+
'Matilda'
3445
);
35-
console.log(allowedShoppingCart);
46+
const newBudget3 = addExpense(newBudget2, spendingLimits, 200, 'Stuff', 'Jay');
47+
48+
// const checkExpenses2 = function (state, limits) {
49+
// return state.map(entry => {
50+
// return entry.value < -getLimit(limits, entry.user)
51+
// ? { ...entry, flag: 'limit' }
52+
// : entry;
53+
// });
54+
// // for (const entry of newBudget3)
55+
// // if (entry.value < -getLimit(limits, entry.user)) entry.flag = 'limit';
56+
// };
57+
58+
const checkExpenses = (state, limits) =>
59+
state.map(entry =>
60+
entry.value < -getLimit(limits, entry.user)
61+
? { ...entry, flag: 'limit' }
62+
: entry
63+
);
64+
65+
const finalBudget = checkExpenses(newBudget3, spendingLimits);
66+
console.log(finalBudget);
67+
68+
// Impure
69+
const logBigExpenses = function (state, bigLimit) {
70+
const bigExpenses = state
71+
.filter(entry => entry.value <= -bigLimit)
72+
.map(entry => entry.description.slice(-2))
73+
.join(' / ');
74+
// .reduce((str, cur) => `${str} / ${cur.description.slice(-2)}`, '');
3675

37-
const createOrderDescription = function (cart) {
38-
const [{ product: p, quantity: q }] = cart;
76+
console.log(bigExpenses);
3977

40-
return `Order with ${q} ${p}${cart.length > 1 ? ', etc...' : '.'}`;
78+
// let output = '';
79+
// for (const entry of budget)
80+
// output +=
81+
// entry.value <= -bigLimit ? `${entry.description.slice(-2)} / ` : '';
82+
// output = output.slice(0, -2); // Remove last '/ '
83+
// console.log(output);
4184
};
42-
const orderDescription = createOrderDescription(allowedShoppingCart);
4385

44-
console.log(orderDescription);
86+
logBigExpenses(finalBudget, 500);

17-Modern-JS-Modules-Tooling/starter/clean.js

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,64 @@
1-
var sc = [
2-
{ product: 'bread', quantity: 6 },
3-
{ product: 'pizza', quantity: 2 },
4-
{ product: 'milk', quantity: 4 },
5-
{ product: 'water', quantity: 10 },
1+
var budget = [
2+
{ value: 250, description: 'Sold old TV 📺', user: 'jonas' },
3+
{ value: -45, description: 'Groceries 🥑', user: 'jonas' },
4+
{ value: 3500, description: 'Monthly salary 👩‍💻', user: 'jonas' },
5+
{ value: 300, description: 'Freelancing 👩‍💻', user: 'jonas' },
6+
{ value: -1100, description: 'New iPhone 📱', user: 'jonas' },
7+
{ value: -20, description: 'Candy 🍭', user: 'matilda' },
8+
{ value: -125, description: 'Toys 🚂', user: 'matilda' },
9+
{ value: -1800, description: 'New Laptop 💻', user: 'jonas' },
610
];
711

8-
var allow = {
9-
lisbon: 5,
10-
others: 7,
12+
var limits = {
13+
jonas: 1500,
14+
matilda: 100,
1115
};
1216

13-
var description = '';
17+
var add = function (value, description, user) {
18+
if (!user) user = 'jonas';
19+
user = user.toLowerCase();
1420

15-
var check = function (city) {
16-
if (sc.length > 0) {
17-
var allowed;
18-
if (city == 'lisbon') {
19-
allowed = allow.lisbon;
21+
var lim;
22+
if (limits[user]) {
23+
lim = limits[user];
24+
} else {
25+
lim = 0;
26+
}
27+
28+
if (value <= lim) {
29+
budget.push({ value: -value, description: description, user: user });
30+
}
31+
};
32+
add(10, 'Pizza 🍕');
33+
add(100, 'Going to movies 🍿', 'Matilda');
34+
add(200, 'Stuff', 'Jay');
35+
console.log(budget);
36+
37+
var check = function () {
38+
for (var el of budget) {
39+
var lim;
40+
if (limits[el.user]) {
41+
lim = limits[el.user];
2042
} else {
21-
allowed = allow.others;
43+
lim = 0;
2244
}
2345

24-
for (item of sc) {
25-
if (item.quantity > allowed) item.quantity = allowed;
46+
if (el.value < -lim) {
47+
el.flag = 'limit';
2648
}
2749
}
2850
};
29-
check('lisbon');
30-
console.log(sc);
51+
check();
3152

32-
var createDescription = function () {
33-
var first = sc[0];
34-
var p = first.product;
35-
var q = first.quantity;
53+
console.log(budget);
3654

37-
if (sc.length > 1) {
38-
description = 'Order with ' + q + ' ' + p + ', etc...';
39-
} else {
40-
description = 'Order with ' + q + ' ' + p + '.';
55+
var bigExpenses = function (limit) {
56+
var output = '';
57+
for (var el of budget) {
58+
if (el.value <= -limit) {
59+
output += el.description.slice(-2) + ' / '; // Emojis are 2 chars
60+
}
4161
}
62+
output = output.slice(0, -2); // Remove last '/ '
63+
console.log(output);
4264
};
43-
createDescription();
44-
45-
console.log(description);

0 commit comments

Comments
 (0)