diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index 8184c83083..8fa71fee5f 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -6,6 +6,31 @@ function isAnagram(str1, str2) { + if(str1.length !== str2.length) return false; + + const countAnagram = {}; + + for(const char of str1) { + const str = char.toLowerCase(); + if(countAnagram[str]) { + countAnagram[str]++; + }else{ + countAnagram[str] = 1; + } + } + + for(const char of str2) { + const str = char.toLowerCase(); + if(countAnagram[str]) { + if(countAnagram[str] === 1) { + delete countAnagram[str]; + }else { + countAnagram[str]--; + } + } + } + + return Object.keys(countAnagram).length === 0; } module.exports = isAnagram; diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 09c4013040..d801952166 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -14,7 +14,20 @@ */ function calculateTotalSpentByCategory(transactions) { - return []; + const result = transactions.reduce((acc, item) => { + if(acc[item.category]) { + acc[item.category] = acc[[item.category]] + item.price; + }else{ + acc[item.category] = item.price; + } + return acc; + }, {}); + + return Object.keys(result).map((item) => ({ + category: item, + totalSpent: result[item] + })) + } module.exports = calculateTotalSpentByCategory; diff --git a/01-js/easy/findLargestElement.js b/01-js/easy/findLargestElement.js index 33278de43a..8168e34cce 100644 --- a/01-js/easy/findLargestElement.js +++ b/01-js/easy/findLargestElement.js @@ -6,7 +6,11 @@ */ function findLargestElement(numbers) { - + if(!numbers.length) return undefined; + return numbers.reduce((acc, item) => { + if(item > acc) acc = item; + return acc; + }, Number.NEGATIVE_INFINITY) } module.exports = findLargestElement; \ No newline at end of file diff --git a/01-js/hard/calculator.js b/01-js/hard/calculator.js index fb60142b79..fe72381edf 100644 --- a/01-js/hard/calculator.js +++ b/01-js/hard/calculator.js @@ -16,6 +16,138 @@ Once you've implemented the logic, test your code by running */ -class Calculator {} +class Calculator { + result; + + constructor() { + this.result = 0; + } + + add(num) { + this.result += num; + } + + sub(num) { + this.result -= num; + } + + multiply(num) { + this.result *= num; + } + + divide(num) { + this.result /= num; + } + + clear() { + this.result = 0; + } + + getResult() { + return this.result; + } + + calculate(exp) { + const tokens = exp.split(" "); + const stack = []; + const precedence = { + "+": 1, + "-": 1, + "*": 2, + "/": 2, + }; + + for (const token of tokens) { + if (!isNaN(token)) { + stack.push(Number(token)); + } else if (token in precedence) { + while ( + stack.length > 0 && + precedence[stack[stack.length - 1]] >= precedence[token] + ) { + const op = stack.pop(); + const b = stack.pop(); + const a = stack.pop(); + switch (op) { + case "+": + stack.push(a + b); + break; + case "-": + stack.push(a - b); + break; + case "*": + stack.push(a * b); + break; + case "/": + stack.push(a / b); + break; + } + } + stack.push(token); + } else if (token === "(") { + stack.push(token); + } else if (token === ")") { + while (stack.length > 0 && stack[stack.length - 1] !== "(") { + const op = stack.pop(); + const b = stack.pop(); + const a = stack.pop(); + switch (op) { + case "+": + stack.push(a + b); + break; + case "-": + stack.push(a - b); + break; + case "*": + stack.push(a * b); + break; + case "/": + stack.push(a / b); + break; + } + } + stack.pop(); + } else { + throw new Error("Invalid character: " + token); + } + } + while (stack.length > 0) { + const op = stack.pop(); + const b = stack.pop(); + const a = stack.pop(); + switch (op) { + case "+": + stack.push(a + b); + break; + case "-": + stack.push(a - b); + break; + case "*": + stack.push(a * b); + break; + case "/": + stack.push(a / b); + break; + } + } + return stack[0]; + } + +} + + +const calc = new Calculator(); +calc.add(10); +console.log(calc.getResult()); +calc.sub(2); +console.log(calc.getResult()); +calc.multiply(6); +console.log(calc.getResult()); +calc.divide(2); +console.log(calc.getResult()); +calc.clear(); +console.log(calc.getResult()); + + module.exports = Calculator; diff --git a/01-js/hard/todo-list.js b/01-js/hard/todo-list.js index 381d6d0754..fba225a18d 100644 --- a/01-js/hard/todo-list.js +++ b/01-js/hard/todo-list.js @@ -12,6 +12,60 @@ class Todo { + todos; + constructor(){ + this.todos = []; + } + + add(todo) { + this.todos.push(todo); + } + + remove(index) { + const todos = this.todos.filter((i, idx) => idx !== index); + this.todos = todos; + } + + update(idx, updatedTodo) { + const todos = this.todos.map((item, index) => { + if(index === idx) { + return { + ...updatedTodo + } + } + return item; + }) + this.todos = todos; + } + + getAll() { + return this.todos; + } + + get(index) { + return this.todos.find((i, idx) => idx === index); + } + + clear() { + this.todos = []; + } } + +const todos = new Todo(); +todos.add({id:1, name:'item 1'}); +todos.add({id:1, name:'item 3'}); +todos.add({id:1, name:'item 2'}); +todos.add({id:1, name:'item 4'}); +todos.add({id:1, name:'item 5'}); +console.log(todos.getAll()); +console.log(todos.get(3)); +todos.update(1, {id: 2, name: 'item 2'}) +todos.remove(3) +todos.clear() +todos.add({id:1, name:'item 5'}); +console.log(todos.getAll()); + + + module.exports = Todo; diff --git a/01-js/medium/countVowels.js b/01-js/medium/countVowels.js index 49db425d63..ab96185cd9 100644 --- a/01-js/medium/countVowels.js +++ b/01-js/medium/countVowels.js @@ -6,7 +6,12 @@ */ function countVowels(str) { - // Your code here + let count = 0; + for(const char of str) { + const vowels = ['a', 'e', 'i', 'o', 'u']; + if(vowels.includes(char.toLowerCase())) count++; + } + return count; } module.exports = countVowels; \ No newline at end of file diff --git a/01-js/medium/palindrome.js b/01-js/medium/palindrome.js index 3f3b9adadb..f3be196789 100644 --- a/01-js/medium/palindrome.js +++ b/01-js/medium/palindrome.js @@ -4,6 +4,23 @@ */ function isPalindrome(str) { + + let newString = ''; + for(const s of str) { + const ch = s.toLowerCase(); + if((ch >= 'a' && ch <= 'z')) { + newString += ch; + } + } + + let i = 0, j = newString.length - 1; + while(i <= j) { + const ch1 = newString.charAt(i); + const ch2 = newString.charAt(j); + if(ch1 !== ch2) return false; + i++; + j--; + } return true; } diff --git a/01-js/medium/times.js b/01-js/medium/times.js index ea2d4c170a..d9ae7e2e1c 100644 --- a/01-js/medium/times.js +++ b/01-js/medium/times.js @@ -9,5 +9,18 @@ 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 start = Date.now(); + let sum = 0; + for(let i = 1; i <= n; i++) { + sum += i; + } + const end = Date.now(); + const timeInSeconds = (end - start) / 1000; + console.log(`Total time = ${timeInSeconds} seconds`); + return sum; +} + +console.log(calculateTime(100)); +console.log(calculateTime(100000)); +console.log(calculateTime(1000000000)); +