Skip to content
4 changes: 4 additions & 0 deletions week-2/week-2-async-js/easy/1-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let count = 1;
setInterval(() => {
console.log(`${count++}`);
}, 1000);
7 changes: 7 additions & 0 deletions week-2/week-2-async-js/easy/2-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let count = 1;
function counter(){
console.log(`${count++}`);
setTimeout(counter, 1000);
}

counter()
12 changes: 12 additions & 0 deletions week-2/week-2-async-js/easy/3-read-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const fs = require("fs");

const content = fs.readFile('a.txt', 'utf-8', (err, data) => {
console.log(`File Content:\n${data}`);
})

console.log("Expensive Operation ongoing...");
for(i=0; i<1000000000; i++){

}
console.log("Expensive Operation done.\n");

7 changes: 7 additions & 0 deletions week-2/week-2-async-js/easy/4-write-to-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const fs = require("fs");

fs.writeFile("a.txt", "Hey this is just another follow up!", "utf-8", () => {
console.log("Done.");
});

console.log("Writing to file ...");
1 change: 1 addition & 0 deletions week-2/week-2-async-js/easy/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hey this is just another follow up!
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/

function wait(n) {
return new Promise((resolve, reject) => {
setTimeout( resolve, n * 1000);
});
}

module.exports = wait;
6 changes: 6 additions & 0 deletions week-2/week-2-async-js/hard (promises)/2-sleep-completely.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/

function sleep(milliseconds) {
return new Promise((resolve, reject) => {
const startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliseconds) {
resolve();
}
});
}

module.exports = sleep;
12 changes: 7 additions & 5 deletions week-2/week-2-async-js/hard (promises)/3-promise-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
*/

function wait1(t) {

return new Promise((resolve) => setTimeout(resolve, t * 1000));
}

function wait2(t) {

return new Promise((resolve) => setTimeout(resolve, t * 1000));
}

function wait3(t) {

return new Promise((resolve) => setTimeout(resolve, t * 1000));
}

function calculateTime(t1, t2, t3) {

async function calculateTime(t1, t2, t3) {
const startTime = Date.now();
await Promise.all([wait1(t1), wait2(t2), wait3(t3)]);
return Date.now() - startTime;
}

module.exports = calculateTime;
20 changes: 17 additions & 3 deletions week-2/week-2-async-js/hard (promises)/4-promise-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@
*/

function wait1(t) {

return new Promise((resolve) => setTimeout(resolve, t * 1000));
}

function wait2(t) {

return new Promise((resolve) => setTimeout(resolve, t * 1000));
}

function wait3(t) {

return new Promise((resolve) => setTimeout(resolve, t * 1000));
}

function calculateTime(t1, t2, t3) {
const startTime = Date.now();

return call(t1, t2, t3).then(() => {
return Date.now() - startTime;
});
}

function call(t1, t2, t3) {
return wait1(t1)
.then(() => {
return wait2(t2);
})
.then(() => {
return wait3(t3);
});
}

module.exports = calculateTime;
17 changes: 17 additions & 0 deletions week-2/week-2-async-js/medium/1-file-cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');

fs.readFile('a.txt', 'utf-8', (err, data) => {
console.log(`Before clening: \n${data}`);
cleanFile(data);
})

function cleanFile(content){
content = content.replace(/\s{2,}/g, ' ')
fs.writeFile('a.txt', content, 'utf-8', () => {
console.log('File cleaned!');
fs.readFile("a.txt", "utf-8", (err, data) => {
console.log(data);
});

})
}
34 changes: 34 additions & 0 deletions week-2/week-2-async-js/medium/2-clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const date = new Date();
setInterval(() => {
console.log(
`24 Hour Format - ${date
.getHours()
.toString()
.padStart(2, "0")}:${date
.getMinutes()
.toString()
.padStart(2, "0")}:${date
.getSeconds()
.toString()
.padStart(2, "0")}`
);
//12 hour format
const hour =
date.getHours() - 12 < 0
? date.getHours() == 0
? 12
: date.getHours()
: date.getHours() - 12;
const flag = date.getHours() > 11 ? "PM" : "AM";
console.log(
`12 Hour Format - ${hour
.toString()
.padStart(2, "0")}:${date
.getMinutes()
.toString()
.padStart(2, "0")}:${date
.getSeconds()
.toString()
.padStart(2, "0")} ${flag}`
);
}, 1000);
1 change: 1 addition & 0 deletions week-2/week-2-async-js/medium/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hi this is TankCodes.
9 changes: 8 additions & 1 deletion week-2/week-2-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
*/

function isAnagram(str1, str2) {

function sortString(str) {
return str.toLowerCase().split("").sort().join("").replace(/\s/g, "");
}
return (sortString(str1) === sortString(str2))
}

console.log(isAnagram("spar", "raps")); //true
console.log(isAnagram("spar", "rpas")); //true
console.log(isAnagram("new york times", "monkeys write")); //true

module.exports = isAnagram;
139 changes: 137 additions & 2 deletions week-2/week-2-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,144 @@
}
Output - [{ category: 'Food', totalSpent: 10 }] // Can have multiple categories, only one example is mentioned here
*/
//Brute force
/* function calculateTotalSpentByCategory(transactions) {
let totalSpent = []
for (const transaction of transactions) {
let flag = true;
for(const category of totalSpent){
if (transaction.category === category.category){
category.totalSpent += transaction.price
flag = false
}
}
if (flag){
const newCategory = {
category: transaction.category,
totalSpent: transaction.price
}
totalSpent.push(newCategory);
}
}
return totalSpent;
} */

function calculateTotalSpentByCategory(transactions) {
return [];
//Optimized
function calculateTotalSpentByCategory(transactions){
const categoryTotals = {}
for(const transaction of transactions){
categoryTotals[transaction.category] = (categoryTotals[transaction.category] || 0) + transaction.price
}
const result = [];
for(const category in categoryTotals){
result.push({
category: category,
totalSpent: categoryTotals[category]
})
}
return result
}

const transactions = [
{
id: 1,
timestamp: 1656076800000,
price: 149,
category: "Food",
itemName: "Dinner",
},
{
id: 2,
timestamp: 1656076800000,
price: 110,
category: "Food",
itemName: "Lunch",
},
{
id: 3,
timestamp: 1656076800000,
price: 459,
category: "Entertainment",
itemName: "Movie",
},
{
id: 4,
timestamp: 1656076800000,
price: 30,
category: "Food",
itemName: "Snacks",
},
{
id: 5,
timestamp: 1656076800000,
price: 20,
category: "Transport",
itemName: "Train",
},
{
id: 6,
timestamp: 1656076800000,
price: 200,
category: "Food",
itemName: "Dinner",
},
{
id: 7,
timestamp: 1656076800000,
price: 50,
category: "Food",
itemName: "Lunch",
},
{
id: 8,
timestamp: 1656076800000,
price: 340,
category: "Food",
itemName: "Pizza",
},
{
id: 9,
timestamp: 1656076800000,
price: 200,
category: "Entertainment",
itemName: "Movie",
},
{
id: 10,
timestamp: 1656076800000,
price: 15,
category: "Transport",
itemName: "Train",
},
{
id: 11,
timestamp: 1656076800000,
price: 15,
category: "Transport",
itemName: "Bus",
},
{
id: 12,
timestamp: 1656076800000,
price: 249,
category: "Entertainment",
itemName: "Netflix",
},
{
id: 13,
timestamp: 1656076800000,
price: 30,
category: "Transport",
itemName: "Auto",
},
{
id: 14,
timestamp: 1656076800000,
price: 100,
category: "Food",
itemName: "Momos",
},
];
console.log(calculateTotalSpentByCategory(transactions));

module.exports = calculateTotalSpentByCategory;
11 changes: 10 additions & 1 deletion week-2/week-2-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
*/

function findLargestElement(numbers) {

let result = numbers[0];
for(const number of numbers){
if (number > result){
result = number;
}
}
return result
}
const numbers = [1, 2, 3, 4, 5, 6, 1, 3, 99, 1, 3, 4234, 5, 2];
console.log(findLargestElement(numbers));


module.exports = findLargestElement;
Loading