Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main 1 #326

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
38 changes: 35 additions & 3 deletions 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 16 additions & 5 deletions 02-async-js/easy/3-read-from-file.md
Original file line number Diff line number Diff line change
@@ -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);
}
23 changes: 22 additions & 1 deletion 02-async-js/easy/4-write-to-file.md
Original file line number Diff line number Diff line change
@@ -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.
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);
}
16 changes: 15 additions & 1 deletion 02-async-js/medium/1-file-cleaner.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,18 @@ After the program runs, the output should be

```
hello world my name is raman
```

```


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);
});