diff --git a/week-2/week-2-async-js/easy/1-counter.js b/week-2/week-2-async-js/easy/1-counter.js new file mode 100644 index 000000000..9501d6736 --- /dev/null +++ b/week-2/week-2-async-js/easy/1-counter.js @@ -0,0 +1,4 @@ +let count = 1; +setInterval(() => { + console.log(`${count++}`); +}, 1000); \ No newline at end of file diff --git a/week-2/week-2-async-js/easy/2-counter.js b/week-2/week-2-async-js/easy/2-counter.js new file mode 100644 index 000000000..ed79a7740 --- /dev/null +++ b/week-2/week-2-async-js/easy/2-counter.js @@ -0,0 +1,7 @@ +let count = 1; +function counter(){ + console.log(`${count++}`); + setTimeout(counter, 1000); +} + +counter() diff --git a/week-2/week-2-async-js/easy/3-read-from-file.js b/week-2/week-2-async-js/easy/3-read-from-file.js new file mode 100644 index 000000000..aaf216069 --- /dev/null +++ b/week-2/week-2-async-js/easy/3-read-from-file.js @@ -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"); + diff --git a/week-2/week-2-async-js/easy/4-write-to-file.js b/week-2/week-2-async-js/easy/4-write-to-file.js new file mode 100644 index 000000000..11513e884 --- /dev/null +++ b/week-2/week-2-async-js/easy/4-write-to-file.js @@ -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 ..."); \ No newline at end of file diff --git a/week-2/week-2-async-js/easy/a.txt b/week-2/week-2-async-js/easy/a.txt new file mode 100644 index 000000000..3057b31ff --- /dev/null +++ b/week-2/week-2-async-js/easy/a.txt @@ -0,0 +1 @@ +Hey this is just another follow up! \ No newline at end of file diff --git a/week-2/week-2-async-js/hard (promises)/1-promisify-setTimeout.js b/week-2/week-2-async-js/hard (promises)/1-promisify-setTimeout.js index 32a99c83f..a62fc882a 100644 --- a/week-2/week-2-async-js/hard (promises)/1-promisify-setTimeout.js +++ b/week-2/week-2-async-js/hard (promises)/1-promisify-setTimeout.js @@ -3,6 +3,9 @@ */ function wait(n) { + return new Promise((resolve, reject) => { + setTimeout( resolve, n * 1000); + }); } module.exports = wait; diff --git a/week-2/week-2-async-js/hard (promises)/2-sleep-completely.js b/week-2/week-2-async-js/hard (promises)/2-sleep-completely.js index a171170b0..6c61ce9bc 100644 --- a/week-2/week-2-async-js/hard (promises)/2-sleep-completely.js +++ b/week-2/week-2-async-js/hard (promises)/2-sleep-completely.js @@ -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; diff --git a/week-2/week-2-async-js/hard (promises)/3-promise-all.js b/week-2/week-2-async-js/hard (promises)/3-promise-all.js index a57838ade..aab256393 100644 --- a/week-2/week-2-async-js/hard (promises)/3-promise-all.js +++ b/week-2/week-2-async-js/hard (promises)/3-promise-all.js @@ -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; diff --git a/week-2/week-2-async-js/hard (promises)/4-promise-chain.js b/week-2/week-2-async-js/hard (promises)/4-promise-chain.js index 6044e241f..77a2dfaac 100644 --- a/week-2/week-2-async-js/hard (promises)/4-promise-chain.js +++ b/week-2/week-2-async-js/hard (promises)/4-promise-chain.js @@ -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; diff --git a/week-2/week-2-async-js/medium/1-file-cleaner.js b/week-2/week-2-async-js/medium/1-file-cleaner.js new file mode 100644 index 000000000..4e598903d --- /dev/null +++ b/week-2/week-2-async-js/medium/1-file-cleaner.js @@ -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); + }); + + }) +} \ No newline at end of file diff --git a/week-2/week-2-async-js/medium/2-clock.js b/week-2/week-2-async-js/medium/2-clock.js new file mode 100644 index 000000000..44ece8f19 --- /dev/null +++ b/week-2/week-2-async-js/medium/2-clock.js @@ -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); diff --git a/week-2/week-2-async-js/medium/a.txt b/week-2/week-2-async-js/medium/a.txt new file mode 100644 index 000000000..5350a71cc --- /dev/null +++ b/week-2/week-2-async-js/medium/a.txt @@ -0,0 +1 @@ +Hi this is TankCodes. \ No newline at end of file diff --git a/week-2/week-2-js/easy/anagram.js b/week-2/week-2-js/easy/anagram.js index 8184c8308..b186d3ee1 100644 --- a/week-2/week-2-js/easy/anagram.js +++ b/week-2/week-2-js/easy/anagram.js @@ -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; diff --git a/week-2/week-2-js/easy/expenditure-analysis.js b/week-2/week-2-js/easy/expenditure-analysis.js index 09c401304..60926ddf2 100644 --- a/week-2/week-2-js/easy/expenditure-analysis.js +++ b/week-2/week-2-js/easy/expenditure-analysis.js @@ -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; diff --git a/week-2/week-2-js/easy/findLargestElement.js b/week-2/week-2-js/easy/findLargestElement.js index 33278de43..8ea883a5b 100644 --- a/week-2/week-2-js/easy/findLargestElement.js +++ b/week-2/week-2-js/easy/findLargestElement.js @@ -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; \ No newline at end of file diff --git a/week-2/week-2-js/hard/calculator.js b/week-2/week-2-js/hard/calculator.js index fb60142b7..9b316986a 100644 --- a/week-2/week-2-js/hard/calculator.js +++ b/week-2/week-2-js/hard/calculator.js @@ -16,6 +16,52 @@ Once you've implemented the logic, test your code by running */ -class Calculator {} +class Calculator { + constructor(result = 0) { + this.result = result; + } + add(num) { + this.result += num; + } + subtract(num) { + this.result -= num; + } + multiply(num) { + this.result *= num; + } + divide(num) { + if (num === 0) throw new Error("Division by Zero"); + this.result /= num; + } + clear() { + this.result = 0; + } + getResult() { + return this.result; + } + calculate(expression) { + if (expression.match(/[a-zA-Z!@#$^&~`|:;'",\?\\<>{}\[\]]/g)) { + throw new Error("Invalid Expression"); + } + expression = expression.replace(/\s/g, ""); + + const result = eval(expression); + if (typeof result !== "number" || !isFinite(result)) { + throw new Error("Invalid Expression"); + } + this.result = result + } +} +const calculator = new Calculator() +calculator.add(5); +console.log(calculator.getResult()); +calculator.subtract(2); +console.log(calculator.getResult()); +calculator.multiply(3); +console.log(calculator.getResult()); +calculator.divide(9); +console.log(calculator.getResult()); +calculator.calculate('10 + 2 * (6 - (4 + 1) / 2) + 7'); +console.log(calculator.getResult()); module.exports = Calculator; diff --git a/week-2/week-2-js/hard/todo-list.js b/week-2/week-2-js/hard/todo-list.js index 381d6d075..852771721 100644 --- a/week-2/week-2-js/hard/todo-list.js +++ b/week-2/week-2-js/hard/todo-list.js @@ -11,7 +11,57 @@ */ class Todo { - + constructor() { + this.todo = []; + } + add(task) { + this.todo.push(task); + } + remove(indexOfTodo) { + this.todo = this.todo.filter((task) => task !== this.todo[indexOfTodo]); + } + update(index, task) { + if (index >= 0 && index < this.todo.length) { + this.todo[index] = task; + } + } + getAll() { + return this.todo; + } + get(indexOfTodo) { + if (indexOfTodo >= 0 && indexOfTodo < this.todo.length) { + return this.todo[indexOfTodo]; + }else{ + return null + } + } + clear() { + this.todo = []; + } } +const todo = new Todo(); + +//add +todo.add("Complete Week 3 assignment"); +todo.add("Complete Week 4 assignment"); +todo.add("Complete Week 5 assignment"); +todo.add("Complete Week 6 assignment"); +console.log(todo.getAll()); + +//remove +todo.remove(1); +console.log(todo.getAll()); + +//update +todo.update(2, "Watch Cohort videos"); +console.log(todo.getAll()); + +//get task +console.log(todo.get(2)); + +//clear todo +todo.clear(); +console.log(todo.getAll()); + module.exports = Todo; diff --git a/week-2/week-2-js/medium/countVowels.js b/week-2/week-2-js/medium/countVowels.js index 49db425d6..19ab270fd 100644 --- a/week-2/week-2-js/medium/countVowels.js +++ b/week-2/week-2-js/medium/countVowels.js @@ -5,8 +5,25 @@ Once you've implemented the logic, test your code by running */ -function countVowels(str) { +/* function countVowels(str) { // Your code here + const vowels = {"a": 1, "e" : 1, "i" : 1, "o" : 1, "u" : 1}; + let count = 0; + for(let character of str){ + character = character.toLowerCase(); + if (vowels[character]){ + count += 1; + } + } + return count +} */ + +//using regex +const countVowels = str => { + return (str.match(/[aioue]/gi) || []).length; } +console.log(countVowels("abcdefg")); + + module.exports = countVowels; \ No newline at end of file diff --git a/week-2/week-2-js/medium/palindrome.js b/week-2/week-2-js/medium/palindrome.js index 3f3b9adad..c9f1f515b 100644 --- a/week-2/week-2-js/medium/palindrome.js +++ b/week-2/week-2-js/medium/palindrome.js @@ -4,7 +4,10 @@ */ function isPalindrome(str) { - return true; + return str.toLowerCase().replace(/[!?,.\s]/g, '') === str.split('').reverse().join('').toLowerCase().replace(/[!?,.\s]/g, ''); } +console.log(isPalindrome('naman')); + + module.exports = isPalindrome; diff --git a/week-2/week-2-js/medium/times.js b/week-2/week-2-js/medium/times.js index ea2d4c170..72e8d8b38 100644 --- a/week-2/week-2-js/medium/times.js +++ b/week-2/week-2-js/medium/times.js @@ -7,7 +7,19 @@ Try running it for Hint - use Date class exposed in JS There is no automated test for this one, this is more for you to understand time goes up as computation goes up */ - +function sum (num){ + let total = 0 + for (i = 1; i <= num; i++){ + total += i + } + return total +} function calculateTime(n) { - return 0.01; -} \ No newline at end of file + const currentTime = new Date(); + sum(n); + return new Date() - currentTime; +} + +console.log(calculateTime(100)); +console.log(calculateTime(100000)); +console.log(calculateTime(1000000000)); diff --git a/week-3/easy/bg-color-changer/index.html b/week-3/easy/bg-color-changer/index.html new file mode 100644 index 000000000..771411846 --- /dev/null +++ b/week-3/easy/bg-color-changer/index.html @@ -0,0 +1,64 @@ + + +
+ + +