diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 20fbb943..0f7b79e0 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -8,8 +8,40 @@ - `npm run test-expenditure-analysis` */ -function calculateTotalSpentByCategory(transactions) { - return []; + +function solve(transactions) { + var spendEstimates = {}; + for (var i = 0; i < transactions.length; i++) { + var t = transactions[i]; + if (spendEstimates[t.category]) { + spendEstimates[t.category] = spendEstimates[t.category] + t.price; + } else { + spendEstimates[t.category] = t.price; + } + } + console.log(spendEstimates); } -module.exports = calculateTotalSpentByCategory; +const transactions = [ + { + category: "food", + itemNames: "maggi", + price: 20, + timestamp: "2023-06-01T10:30:00Z", + }, + { + category: "drink", + itemNames: "coke", + price: 40, + timestamp: "2023-06-01T10:30:00Z", + }, + { + category: "drink", + itemNames: "lassi", + price: 50, + timestamp: "2023-06-01T10:30:00Z", + }, +]; + + +solve(transactions) diff --git a/02-async-js/easy/3-read-from-file.md b/02-async-js/easy/3-read-from-file.md index f619d71a..41eb1bf7 100644 --- a/02-async-js/easy/3-read-from-file.md +++ b/02-async-js/easy/3-read-from-file.md @@ -1,7 +1,18 @@ -## Reading the contents of a file +const fs = require('fs') -Write code to read contents of a file and print it to the console. -You can use the fs library to as a black box, the goal is to understand async tasks. -Try to do an expensive operation below the file read and see how it affects the output. -Make the expensive operation more and more expensive and see how it affects the output. +// async way of readin a file +fs.readFile('read.txt', 'utf8', (err, data) => { + if (err) { + console.error('Error reading file:', err); + return; + } + console.log('File contents:', data); +}); +// Synchronous file read +try { + const data = fs.readFileSync('read.txt', 'utf8'); + console.log('File contents:', data); +} catch (err) { + console.error('Error reading file:', err); +} diff --git a/02-async-js/easy/4-write-to-file.md b/02-async-js/easy/4-write-to-file.md index f1c151f9..0b869494 100644 --- a/02-async-js/easy/4-write-to-file.md +++ b/02-async-js/easy/4-write-to-file.md @@ -1,3 +1,24 @@ ## Write to a file Using the fs library again, try to write to the contents of a file. -You can use the fs library to as a black box, the goal is to understand async tasks. \ No newline at end of file +You can use the fs library to as a black box, the goal is to understand async tasks. + +const fs = require('fs'); + +// Asynchronous file write +const data = 'Hello, World!'; +fs.writeFile('read.txt', data, (err) => { + if (err) { + console.error('Error writing file:', err); + return; + } + console.log('File written successfully!'); +}); + +// Synchronous file write +try { + const data = 'Hello, World!'; + fs.writeFileSync('read.txt', data); + console.log('File written successfully!'); +} catch (err) { + console.error('Error writing file:', err); +} diff --git a/02-async-js/medium/1-file-cleaner.md b/02-async-js/medium/1-file-cleaner.md index 2be6e450..b61c3198 100644 --- a/02-async-js/medium/1-file-cleaner.md +++ b/02-async-js/medium/1-file-cleaner.md @@ -10,4 +10,18 @@ After the program runs, the output should be ``` hello world my name is raman -``` \ No newline at end of file + +``` + + +const fs = require('fs') + +// async way of readin a file +fs.readFile('read.txt', 'utf8', (err, data) => { + if (err) { + console.error('Error reading file:', err); + return; + } + const data1 = data.replace(/\s+/g, ' '); + console.log('File contents:', data1); +});