Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions JavaScript/Tasks/1-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// Task: rewrite function to return result into sync callback

// Change signature to: (items, callback(result))
const total = (items) => {
const total = (items, callback) => {
let result = 0;
for (const item of items) {
result += item.price;
}
return result;
callback(result);

};

const electronics = [
Expand All @@ -18,5 +19,4 @@ const electronics = [
];

// Use new signature total(electronics, (money) => ...)
const money = total(electronics);
console.log({ money });
total(electronics, (money) => console.log(money));
11 changes: 9 additions & 2 deletions JavaScript/Tasks/2-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const total = (items, callback) => {
for (const item of items) {
result += item.price;
}
callback(result);
if(result < 0){
callback(new Error("Price is negative"), result)
}
callback(null, result);
};

const electronics = [
Expand All @@ -17,6 +20,10 @@ const electronics = [
{ name: 'HDMI cable', price: 10 },
];

total(electronics, (money) => {
total(electronics, (err, money) => {
if(err){
console.error(err.message, money)
throw err
}
console.log({ money });
});
33 changes: 23 additions & 10 deletions JavaScript/Tasks/3-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@
// { money: 1610 }
// { money: 1610 }

const total = (items, callback) => {
const total = async (items, callback) => {
let result = 0;
for (const item of items) {
console.log({ check: { item } });
if (item.price < 0) {
callback(new Error('Negative price is not allowed'));
return;
}
result += item.price;
}
callback(null, result);
let index = 0;
await new Promise((res, rej)=> {
const timerId = setInterval(()=>{
const item = items[index];
if(!item){
clearInterval(timerId)
res();
return
}
console.log({ check: { item } });
if (item.price < 0) {
rej(new Error('Negative price is not allowed'));
clearInterval(timerId)
return;
}
result += item.price;
index+=1;
}, 1000)


}).then(()=>callback(null, result)).catch(err=>callback(err))

};

const electronics = [
Expand Down
28 changes: 17 additions & 11 deletions JavaScript/Tasks/4-hell.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ const budget = (limit) => {

const wallet = budget(1650);

getPurchase((purchase) => {
let amount = 0;
iterateGroups(purchase, (group) => {
groupTotal(group, (subtotal) => {
wallet.withdraw(subtotal, (success) => {
if (success) amount += subtotal;
wallet.rest((balance) => {
console.log({ success, amount, subtotal, balance });
});
});
const processGroupTotal = (amount) => (subtotal) => {
wallet.withdraw(subtotal, (success) => {
if (success) amount += subtotal;
wallet.rest((balance) => {
console.log({ success, amount, subtotal, balance });
});
});
});
}

const processIterateGroups = (amount) =>(group) => {
groupTotal(group, processGroupTotal(amount) );
}

const processPurchase = (purchase) => {
let amount = 0;
iterateGroups(purchase, processIterateGroups(amount));
}

getPurchase(processPurchase);
41 changes: 25 additions & 16 deletions JavaScript/Tasks/5-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,39 @@ const calculateSubtotal = (goods, callback) => {
let amount = 0;
for (const item of goods) {
if (typeof item.name !== 'string') {
throw new Error('Noname in item in the bill');
return callback(new Error('Noname in item in the bill'))
}
if (typeof item.price !== 'number') {
throw new Error(`${item.name} price expected to be number`);
return callback(new Error(`${item.name} price expected to be number`));
}
if (item.price < 0) {
throw new Error(`Negative price for ${item.name}`);
return callback(new Error(`Negative price for ${item.name}`));
}
amount += item.price;
}
callback(amount);
callback(null, amount);
};

const calculateTotal = (order, callback) => {
const expenses = new Map();
let total = 0;
const calc = (groupName) => (amount) => {
total += amount;
expenses.set(groupName, amount);
const calc = (groupName) => (err, amount) => {
if(err){
return callback(err);
} else {
total += amount;
expenses.set(groupName, amount);
}

};
for (const groupName in order) {
const goods = order[groupName];
calculateSubtotal(goods, calc(groupName));
if (total > MAX_PURCHASE) {
throw new Error('Total is above the limit');
return callback(new Error('Total is above the limit'));
}
}
return callback({ total, expenses });
return callback(null, { total, expenses });
};

const purchase = {
Expand All @@ -56,12 +61,16 @@ const purchase = {
],
};

try {

console.log(purchase);
calculateTotal(purchase, (bill) => {
console.log(bill);
calculateTotal(purchase, (err, bill) => {

if(err){
console.log('Error detected');
console.error(err);
} else {
console.log(bill);
}

});
} catch (error) {
console.log('Error detected');
console.error(error);
}