From 1b27be9f548c4175b9fc82a52c0e53e51e4bd179 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Tue, 2 Dec 2025 10:50:20 +0500 Subject: [PATCH 1/5] fix 1-sync task --- JavaScript/Tasks/1-sync.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/JavaScript/Tasks/1-sync.js b/JavaScript/Tasks/1-sync.js index 9939c4f..12a38e0 100644 --- a/JavaScript/Tasks/1-sync.js +++ b/JavaScript/Tasks/1-sync.js @@ -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 = [ @@ -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)); From c9865cd868b8e03749974f600090f20854869e99 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Tue, 2 Dec 2025 11:02:38 +0500 Subject: [PATCH 2/5] do 2-error task --- JavaScript/Tasks/2-error.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/JavaScript/Tasks/2-error.js b/JavaScript/Tasks/2-error.js index 5badbcf..e620a3a 100644 --- a/JavaScript/Tasks/2-error.js +++ b/JavaScript/Tasks/2-error.js @@ -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 = [ @@ -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 }); }); From 795e7bda1039bd1602738d3864f2fd79794068c6 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Tue, 2 Dec 2025 11:38:02 +0500 Subject: [PATCH 3/5] do 3 async task --- JavaScript/Tasks/3-async.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/JavaScript/Tasks/3-async.js b/JavaScript/Tasks/3-async.js index 626fe64..1f94823 100644 --- a/JavaScript/Tasks/3-async.js +++ b/JavaScript/Tasks/3-async.js @@ -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 = [ From 688dc7fed31426bf447f5c5448d2a53f382dd9e1 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Tue, 2 Dec 2025 11:51:10 +0500 Subject: [PATCH 4/5] refactor callback hell with closure --- JavaScript/Tasks/4-hell.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/JavaScript/Tasks/4-hell.js b/JavaScript/Tasks/4-hell.js index 0985775..a197773 100644 --- a/JavaScript/Tasks/4-hell.js +++ b/JavaScript/Tasks/4-hell.js @@ -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); From 06a85c06c3d1bda48312324d7a87911a9a362291 Mon Sep 17 00:00:00 2001 From: Bakhodir Kadyrov Date: Tue, 2 Dec 2025 12:34:04 +0500 Subject: [PATCH 5/5] do 5-errors --- JavaScript/Tasks/5-errors.js | 41 ++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/JavaScript/Tasks/5-errors.js b/JavaScript/Tasks/5-errors.js index f5f9769..43549ef 100644 --- a/JavaScript/Tasks/5-errors.js +++ b/JavaScript/Tasks/5-errors.js @@ -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 = { @@ -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); -} +