diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index fff61427..b4904773 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -8,7 +8,12 @@ */ function isAnagram(str1, str2) { + var st1=str1.toLowerCase(); + var st2= str2.toLowerCase(); + if(st1.split('').sort().join('') === st2.split('').sort().join('')) return true; + + else return false; } module.exports = isAnagram; diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 20fbb943..a8d9a69f 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -9,7 +9,31 @@ */ function calculateTotalSpentByCategory(transactions) { - return []; + + var categories={}; + + var output=[]; + + for(var item of transactions) + { + if(categories[item.category]) + { + categories[item.category]=categories[item.category]+item.price + } + + else + { + categories[item.category] = item.price; + } + } + + for(var price in categories) + { + output.push({category: price, totalSpent: categories[price]}); + } + + return output; + } module.exports = calculateTotalSpentByCategory; diff --git a/01-js/hard/calculator.js b/01-js/hard/calculator.js index 82d48229..b341e4ac 100644 --- a/01-js/hard/calculator.js +++ b/01-js/hard/calculator.js @@ -17,6 +17,59 @@ - `npm run test-calculator` */ -class Calculator {} +class Calculator { + + constructor() + { + this.result=0; + } + + add(num) + { + this.result=this.result+num; + } + subtract(num) + { + this.result=this.result-num; + } + multiply(num) + { + this.result=this.result*num; + } + divide(num) + { + if (num === 0) { + throw new Error("Cannot divide by zero"); + } + this.result=this.result/num; + } + clear() + { + this.result=0; + } + getResult() + { + return this.result; + } + calculate(expression) { + expression = expression.replace(/\s+/g, ''); + + if (!/^[0-9+\-*/().]+$/.test(expression)) { + throw new Error("Invalid expression"); + } + + try { + // Check for division by zero + if (expression.includes('/0')) { + throw new Error("Cannot divide by zero"); + } + + this.result = eval(expression); + } catch (error) { + throw new Error("Invalid expression"); + } + } + +} module.exports = Calculator; diff --git a/01-js/hard/todo-list.js b/01-js/hard/todo-list.js index 7c9c1806..e28d666a 100644 --- a/01-js/hard/todo-list.js +++ b/01-js/hard/todo-list.js @@ -13,6 +13,39 @@ class Todo { + constructor() + { + this.todos=[]; + } + + add(todo) + { + this.todos.push(todo); + } + + remove(ind) + { + if(ind>=0 && ind< this.todos.length) this.todos.splice(ind,1); + } + + update(ind,newTodo) + { + if(ind>=0 && ind< this.todos.length) this.todos[ind]= newTodo; + } + getAll() + { + return this.todos; + } + get(ind) + { + if(ind>=0 && ind< this.todos.length) return this.todos[ind]; + + return null; + } + clear() + { + this.todos=[]; + } } module.exports = Todo; diff --git a/01-js/medium/palindrome.js b/01-js/medium/palindrome.js index d8fe2d8f..b11d4ea0 100644 --- a/01-js/medium/palindrome.js +++ b/01-js/medium/palindrome.js @@ -7,6 +7,23 @@ */ function isPalindrome(str) { + var lstr=str.toLowerCase(); + lstr = lstr.replaceAll(" ",""); + + var n=lstr.length; + var i=0,j=n-1; + + while(i'z')i++; + if(lstr[j]<'a' || lstr[j]>'z')j--; + + if(lstr[i]!= lstr[j]) return false; + + i++; + j--; + } + return true; } diff --git a/01-js/medium/times.js b/01-js/medium/times.js index eb125cc2..3ccf597f 100644 --- a/01-js/medium/times.js +++ b/01-js/medium/times.js @@ -8,5 +8,16 @@ Hint - use Date class exposed in JS */ function calculateTime(n) { - return 0.01; -} \ No newline at end of file + var sum=0; + + var curr= parseFloat(Date.now()); + for(var i=1; i<=n; i++) + { + sum+=i; + } + var ncurr= parseFloat(Date.now()); + + return parseFloat(ncurr-curr); +} + +console.log(calculateTime(10000000)); \ No newline at end of file