From 5963dad7d09abb1e726c161873209ee04057a304 Mon Sep 17 00:00:00 2001 From: Aayush Date: Mon, 9 Dec 2024 18:32:42 +0000 Subject: [PATCH] Solved Week 2 assignments --- week-2/week-2-async-js/easy/counter.js | 15 +++++ .../hard (promises)/1-promisify-setTimeout.js | 3 + .../hard (promises)/2-sleep-completely.js | 5 ++ .../hard (promises)/3-promise-all.js | 18 ++++-- .../hard (promises)/4-promise-chain.js | 22 +++++-- week-2/week-2-js/easy/anagram.js | 4 ++ week-2/week-2-js/easy/expenditure-analysis.js | 24 +++++++- week-2/week-2-js/easy/findLargestElement.js | 11 +++- week-2/week-2-js/hard/calculator.js | 59 ++++++++++++++++++- week-2/week-2-js/hard/todo-list.js | 33 +++++++++++ week-2/week-2-js/medium/countVowels.js | 16 ++++- week-2/week-2-js/medium/palindrome.js | 6 ++ week-2/week-2-js/medium/times.js | 16 ++++- 13 files changed, 216 insertions(+), 16 deletions(-) create mode 100644 week-2/week-2-async-js/easy/counter.js diff --git a/week-2/week-2-async-js/easy/counter.js b/week-2/week-2-async-js/easy/counter.js new file mode 100644 index 000000000..2080bf23c --- /dev/null +++ b/week-2/week-2-async-js/easy/counter.js @@ -0,0 +1,15 @@ +let count = 1; +/* +setInterval(() => { + console.log(count); + count++; +}, 1000); + */ + +function counter() { + console.log(count); + count++; + setTimeout(counter, 1000); +} + +counter(); 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..1a303bedb 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..90b97e0fc 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,11 @@ */ 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..3ed29beae 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,29 @@ */ function wait1(t) { - + return new Promise((resolve, reject) => { + setTimeout(resolve, t * 1000); + }); } function wait2(t) { - + return new Promise((resolve, reject) => { + setTimeout(resolve, t * 1000); + }); } function wait3(t) { - + return new Promise((resolve, reject) => { + setTimeout(resolve, t * 1000); + }); } function calculateTime(t1, t2, t3) { - + const startTime = Date.now(); + return Promise.all([wait1(t1), wait2(t2), wait3(t3)]).then(() => { + const endTime = Date.now(); + return endTime - 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..f08ac40ac 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, reject) => { + setTimeout(resolve, t * 1000); + }); } function wait2(t) { - + return new Promise((resolve, reject) => { + setTimeout(resolve, t * 1000); + }); } function wait3(t) { - + return new Promise((resolve, reject) => { + setTimeout(resolve, t * 1000); + }); } function calculateTime(t1, t2, t3) { - + const startTime = Date.now(); + + return wait1(t1) + .then(() => wait2(t2)) + .then(() => wait3(t3)) + .then(() => { + const endTime = Date.now(); + return endTime - startTime; + }); } module.exports = calculateTime; diff --git a/week-2/week-2-js/easy/anagram.js b/week-2/week-2-js/easy/anagram.js index 8184c8308..843f30565 100644 --- a/week-2/week-2-js/easy/anagram.js +++ b/week-2/week-2-js/easy/anagram.js @@ -5,7 +5,11 @@ */ function isAnagram(str1, str2) { + function normalize(str) { + return str.toLowerCase().replace(/\s+/g, "").split("").sort().join(""); + } + return normalize(str1) == normalize(str2); } 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..7e4d50aba 100644 --- a/week-2/week-2-js/easy/expenditure-analysis.js +++ b/week-2/week-2-js/easy/expenditure-analysis.js @@ -14,7 +14,29 @@ */ function calculateTotalSpentByCategory(transactions) { - return []; + let result = []; + + for (let i = 0; i < transactions.length; i++) { + var transaction = transactions[i]; + var categoryExists = false; + + for (let j = 0; j < result.length; j++) { + if (result[j].category == transaction.category) { + result[j].totalSpent += transaction.price; + categoryExists = true; + break; + } + } + + if (!categoryExists) { + result.push({ + category: transaction.category, + totalSpent: transaction.price, + }); + } + } + + return result; } 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..adb3735ab 100644 --- a/week-2/week-2-js/easy/findLargestElement.js +++ b/week-2/week-2-js/easy/findLargestElement.js @@ -6,7 +6,14 @@ */ function findLargestElement(numbers) { - + let max = numbers[0]; + + for (let i = 1; i <= numbers.length; i++) { + if (max < numbers[i]) { + max = numbers[i]; + } + } + return max; } -module.exports = findLargestElement; \ No newline at end of file +module.exports = findLargestElement; diff --git a/week-2/week-2-js/hard/calculator.js b/week-2/week-2-js/hard/calculator.js index fb60142b7..8445eec21 100644 --- a/week-2/week-2-js/hard/calculator.js +++ b/week-2/week-2-js/hard/calculator.js @@ -16,6 +16,63 @@ Once you've implemented the logic, test your code by running */ -class Calculator {} +class Calculator { + constructor() { + this.result = 0; + } + + add(num) { + this.result += num; + return this.result; + } + + subtract(num) { + this.result -= num; + return this.result; + } + + multiply(num) { + this.result *= num; + return this.result; + } + + divide(num) { + if (num == 0) { + throw new Error("Cannot divide by zero"); + } + this.result /= num; + return this.result; + } + + clear() { + this.result = 0; + return this.result; + } + + getResult() { + return this.result; + } + + calculate(inputExpression) { + const expression = inputExpression.replace(/\s+/g, ""); + const isValidExpression = /^[0-9+\-*/().]+$/.test(expression); + + if (!isValidExpression) { + throw new Error("Invalid Expression"); + } + + try { + this.result = eval(expression); + } catch (error) { + throw new Error("Invalid Expression"); + } + + if (this.result == Infinity) { + throw new Error("Cannot divide a number by 0"); + } + + return this.result; + } +} 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..f1aee2f19 100644 --- a/week-2/week-2-js/hard/todo-list.js +++ b/week-2/week-2-js/hard/todo-list.js @@ -11,7 +11,40 @@ */ class Todo { + constructor() { + this.todos = []; + } + add(todo) { + this.todos.push(todo); + } + + remove(indexOfTodo) { + if (indexOfTodo >= 0 && indexOfTodo < this.todos.length) { + this.todos.splice(indexOfTodo, 1); + } + } + + update(indexOfTodo, updatedTodo) { + if (indexOfTodo >= 0 && indexOfTodo < this.todos.length) { + this.todos[indexOfTodo] = updatedTodo; + } + } + + getAll() { + return this.todos; + } + + get(indexOfTodo) { + if (indexOfTodo >= 0 && indexOfTodo < this.todos.length) { + return this.todos[indexOfTodo]; + } + return null; + } + + clear() { + this.todos = []; + } } 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..92761836c 100644 --- a/week-2/week-2-js/medium/countVowels.js +++ b/week-2/week-2-js/medium/countVowels.js @@ -6,7 +6,19 @@ */ function countVowels(str) { - // Your code here + // Your code here + const vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]; + let count = 0; + + for (let i = 0; i < str.length; i++) { + for (let j = 0; j < vowels.length; j++) { + if (str[i] == vowels[j]) { + count++; + } + } + } + + return count; } -module.exports = countVowels; \ No newline at end of file +module.exports = countVowels; diff --git a/week-2/week-2-js/medium/palindrome.js b/week-2/week-2-js/medium/palindrome.js index 3f3b9adad..4a06288b3 100644 --- a/week-2/week-2-js/medium/palindrome.js +++ b/week-2/week-2-js/medium/palindrome.js @@ -4,6 +4,12 @@ */ function isPalindrome(str) { + let str1 = str.replace(/[\W_]/g, "").toLowerCase(); + for (let i = 0; i < str1.length / 2; i++) { + if (str1[i] != str1[str1.length - i - 1]) { + return false; + } + } return true; } diff --git a/week-2/week-2-js/medium/times.js b/week-2/week-2-js/medium/times.js index ea2d4c170..3d1a9ecf3 100644 --- a/week-2/week-2-js/medium/times.js +++ b/week-2/week-2-js/medium/times.js @@ -9,5 +9,17 @@ There is no automated test for this one, this is more for you to understand time */ function calculateTime(n) { - return 0.01; -} \ No newline at end of file + const startTime = new Date().getTime(); + + let sum = 0; + for (let i = 1; i <= n; i++) { + sum += i; + } + const endTime = new Date().getTime(); + + return (endTime - startTime) / 1000; +} + +console.log(calculateTime(100)); +console.log(calculateTime(100000)); +console.log(calculateTime(1000000000));