Skip to content

Commit

Permalink
v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschmedtmann committed Jan 22, 2021
1 parent 9f1ffbc commit d817e0e
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 65 deletions.
24 changes: 24 additions & 0 deletions 09-Data-Structures-Operators/final/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ const restaurant = {
},
};

/*
///////////////////////////////////////
// String Methods Practice
const flights =
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';
// 🔴 Delayed Departure from FAO to TXL (11h25)
// Arrival from BRU to FAO (11h45)
// 🔴 Delayed Arrival from HEL to FAO (12h05)
// Departure from FAO to LIS (12h30)
const getCode = str => str.slice(0, 3).toUpperCase();
for (const flight of flights.split('+')) {
const [type, from, to, time] = flight.split(';');
const output = `${type.startsWith('_Delayed') ? '🔴' : ''}${type.replaceAll(
'_',
' '
)} ${getCode(from)} ${getCode(to)} (${time.replace(':', 'h')})`.padStart(36);
console.log(output);
}
*/

///////////////////////////////////////
// Coding Challenge #4

Expand Down
5 changes: 5 additions & 0 deletions 09-Data-Structures-Operators/starter/script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

// Data needed for a later exercise
const flights =
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';

// Data needed for first part of the section
const restaurant = {
name: 'Classico Italiano',
location: 'Via Angelo Tavanti 23, Firenze, Italy',
Expand Down
64 changes: 63 additions & 1 deletion 11-Arrays-Bankist/final/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ console.log(movements);
const arr = [1, 2, 3, 4, 5, 6, 7];
console.log(new Array(1, 2, 3, 4, 5, 6, 7));
// Emprty arrays + fill method
// Empty arrays + fill method
const x = new Array(7);
console.log(x);
// console.log(x.map(() => 5));
Expand All @@ -636,6 +636,68 @@ labelBalance.addEventListener('click', function () {
const movementsUI2 = [...document.querySelectorAll('.movements__value')];
});
///////////////////////////////////////
// Array Methods Practice
// 1.
const bankDepositSum = accounts
.flatMap(acc => acc.movements)
.filter(mov => mov > 0)
.reduce((sum, cur) => sum + cur, 0);
console.log(bankDepositSum);
// 2.
// const numDeposits1000 = accounts
// .flatMap(acc => acc.movements)
// .filter(mov => mov >= 1000).length;
const numDeposits1000 = accounts
.flatMap(acc => acc.movements)
.reduce((count, cur) => (cur >= 1000 ? ++count : count), 0);
console.log(numDeposits1000);
// Prefixed ++ operator
let a = 10;
console.log(++a);
console.log(a);
// 3.
const { deposits, withdrawals } = accounts
.flatMap(acc => acc.movements)
.reduce(
(sums, cur) => {
// cur > 0 ? (sums.deposits += cur) : (sums.withdrawals += cur);
sums[cur > 0 ? 'deposits' : 'withdrawals'] += cur;
return sums;
},
{ deposits: 0, withdrawals: 0 }
);
console.log(deposits, withdrawals);
// 4.
// this is a nice title -> This Is a Nice Title
const convertTitleCase = function (title) {
const capitalize = str => str[0].toUpperCase() + str.slice(1);
const exceptions = ['a', 'an', 'and', 'the', 'but', 'or', 'on', 'in', 'with'];
const titleCase = title
.toLowerCase()
.split(' ')
.map(word => (exceptions.includes(word) ? word : capitalize(word)))
.join(' ');
return capitalize(titleCase);
};
console.log(convertTitleCase('this is a nice title'));
console.log(convertTitleCase('this is a LONG title but not too long'));
console.log(convertTitleCase('and here is another title with an EXAMPLE'));
*/

///////////////////////////////////////
Expand Down
110 changes: 76 additions & 34 deletions 17-Modern-JS-Modules-Tooling/final/clean.js
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);
79 changes: 49 additions & 30 deletions 17-Modern-JS-Modules-Tooling/starter/clean.js
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);

0 comments on commit d817e0e

Please sign in to comment.