From 08af9c41eacec3160a2feab320a44e8ef28124dd Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Mon, 12 Jun 2023 13:10:17 +0530 Subject: [PATCH 1/8] [week1] added tests, package.json, updated readme --- 01-js/README.md | 4 + 01-js/package.json | 21 +++++ 01-js/tests/anagram.test.js | 19 ++++ 01-js/tests/calculator.test.js | 93 +++++++++++++++++++ 01-js/tests/expenditure-analysis.test.js | 111 +++++++++++++++++++++++ 01-js/tests/palindrome.test.js | 19 ++++ 01-js/tests/todo-list.test.js | 71 +++++++++++++++ 7 files changed, 338 insertions(+) create mode 100644 01-js/package.json create mode 100644 01-js/tests/anagram.test.js create mode 100644 01-js/tests/calculator.test.js create mode 100644 01-js/tests/expenditure-analysis.test.js create mode 100644 01-js/tests/palindrome.test.js create mode 100644 01-js/tests/todo-list.test.js diff --git a/01-js/README.md b/01-js/README.md index 71aae349..122eaf57 100644 --- a/01-js/README.md +++ b/01-js/README.md @@ -15,6 +15,10 @@ Feel free to start doing these in any order you like. 1. Calculator 2. Todo List +## Testing +1. Follow the comment above each problem to run test for that problem +3. To tests for all the problems of this week run ```npx jest ./tests/``` + #### Development Setup 1. If you have Node.js locally, you should run these on your machine 2. If you don't, you can copy these over to repl.it and run it there. diff --git a/01-js/package.json b/01-js/package.json new file mode 100644 index 00000000..a3b592da --- /dev/null +++ b/01-js/package.json @@ -0,0 +1,21 @@ +{ + "name": "01", + "version": "1.0.0", + "description": "You are provided empty JavaScript files (or having function signatures) in this directory. You have to follow the instructions given in each file and write the code in the same file to complete the assignment.", + "main": "anagram.js", + "directories": { + "test": "tests" + }, + "scripts": { + "test-anagram": "npx jest ./tests/anagram.test.js", + "test-calculator": "npx jest ./tests/calculator.test.js", + "test-duplicate-transactions": "npx jest ./tests/duplicate-transactions.test.js", + "test-expenditure-analysis": "npx jest ./tests/expenditure-analysis.test.js", + "test-palindrome": "npx jest ./tests/palindrome.test.js", + "test-todo-list": "npx jest ./tests/todo-list.test.js", + "test-all": "npx jest ./tests/" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/01-js/tests/anagram.test.js b/01-js/tests/anagram.test.js new file mode 100644 index 00000000..fbb56cdf --- /dev/null +++ b/01-js/tests/anagram.test.js @@ -0,0 +1,19 @@ +const isAnagram = require('../easy/anagram'); + +describe('isAnagram', () => { + test('returns true for anagrams', () => { + expect(isAnagram('listen', 'silent')).toBe(true); + expect(isAnagram('rail safety', 'fairy tales')).toBe( + true + ); + expect(isAnagram('openai', 'aiopen')).toBe(true); + expect(isAnagram('', '')).toBe(true); + }); + + test('returns false for non-anagrams', () => { + expect(isAnagram('hello', 'world')).toBe(false); + expect(isAnagram('openai', 'open')).toBe(false); + expect(isAnagram('hello', 'lhel')).toBe(false); + expect(isAnagram('working', 'non')).toBe(false); + }); +}); diff --git a/01-js/tests/calculator.test.js b/01-js/tests/calculator.test.js new file mode 100644 index 00000000..dc3f5fd4 --- /dev/null +++ b/01-js/tests/calculator.test.js @@ -0,0 +1,93 @@ +const Calculator = require('../hard/calculator'); + +describe('Calculator', () => { + let calc; + + beforeEach(() => { + calc = new Calculator(); + }); + + afterEach(() => { + calc.clear(); + }); + + test('addition', () => { + calc.add(5); + expect(calc.getResult()).toBe(5); + + calc.add(3); + expect(calc.getResult()).toBe(8); + }); + + test('subtraction', () => { + calc.subtract(5); + expect(calc.getResult()).toBe(-5); + + calc.subtract(3); + expect(calc.getResult()).toBe(-8); + }); + + test('multiplication', () => { + calc.add(4); + calc.multiply(3); + expect(calc.getResult()).toBe(12); + + calc.multiply(0); + expect(calc.getResult()).toBe(0); + }); + + test('division', () => { + calc.add(12); + + calc.divide(4); + expect(calc.getResult()).toBe(3); + + expect(() => calc.divide(0)).toThrow(Error); + expect(calc.getResult()).toBe(3); + }); + + test('clear', () => { + calc.add(5); + calc.clear(); + expect(calc.getResult()).toBe(0); + }); + + describe('calculate expression method tests suite', () => { + test('calculate addition and multiplication', () => { + calc.calculate('2 + 3 * 4'); + expect(calc.getResult()).toBe(14); + }); + + test('calculate division', () => { + calc.calculate('( 15 + 3) / 6 '); + expect(calc.getResult()).toBe(3); + }); + + test('calculate subtraction', () => { + calc.calculate('10 - (4 + 2)'); + expect(calc.getResult()).toBe(4); + }); + + test('calculate complex expression', () => { + calc.calculate( + '10 + 2 * ( 6 - (4 + 1) / 2) + 7' + ); + expect(calc.getResult()).toBe(24); + }); + + test('calculate expression with decimals', () => { + calc.calculate('(2.5 + 1.5) * 3'); + expect(calc.getResult()).toBe(12); + }); + + test('calculate expression with invalid characters', () => { + expect(() => calc.calculate('5 + abc')).toThrow( + Error + ); + }); + + test('calculate division by zero', () => { + expect(() => calc.calculate('10 / 0')).toThrow(Error); + }); + }); +}); diff --git a/01-js/tests/expenditure-analysis.test.js b/01-js/tests/expenditure-analysis.test.js new file mode 100644 index 00000000..94eb0cd7 --- /dev/null +++ b/01-js/tests/expenditure-analysis.test.js @@ -0,0 +1,111 @@ +const calculateTotalSpentByCategory = require('../easy/expenditure-analysis'); + +describe('calculateTotalSpentByCategory', () => { + test('returns the correct total spent for each category', () => { + const transactions = [ + { + id: 1, + timestamp: 1656076800000, + price: 10, + category: 'Food', + itemName: 'Pizza', + }, + { + id: 2, + timestamp: 1656259600000, + price: 20, + category: 'Food', + itemName: 'Burger', + }, + { + id: 3, + timestamp: 1656019200000, + price: 15, + category: 'Clothing', + itemName: 'T-Shirt', + }, + { + id: 4, + timestamp: 1656364800000, + price: 30, + category: 'Electronics', + itemName: 'Headphones', + }, + { + id: 5, + timestamp: 1656105600000, + price: 25, + category: 'Clothing', + itemName: 'Jeans', + }, + ]; + + const result = + calculateTotalSpentByCategory(transactions); + + expect(result).toEqual([ + { category: 'Food', totalSpent: 30 }, + { category: 'Clothing', totalSpent: 40 }, + { category: 'Electronics', totalSpent: 30 }, + ]); + }); + + test('returns an empty array when given an empty input', () => { + const transactions = []; + const result = + calculateTotalSpentByCategory(transactions); + expect(result).toEqual([]); + }); + + test('returns the correct total spent for a single transaction', () => { + const transactions = [ + { + id: 1, + timestamp: 1656076800000, + price: 10, + category: 'Food', + itemName: 'Pizza', + }, + ]; + + const result = + calculateTotalSpentByCategory(transactions); + + expect(result).toEqual([ + { category: 'Food', totalSpent: 10 }, + ]); + }); + + test('returns the correct total spent when multiple transactions have the same category', () => { + const transactions = [ + { + id: 1, + timestamp: 1656076800000, + price: 10, + category: 'Food', + itemName: 'Pizza', + }, + { + id: 2, + timestamp: 1656105600000, + price: 20, + category: 'Food', + itemName: 'Burger', + }, + { + id: 3, + timestamp: 1656134400000, + price: 30, + category: 'Food', + itemName: 'Sushi', + }, + ]; + + const result = + calculateTotalSpentByCategory(transactions); + + expect(result).toEqual([ + { category: 'Food', totalSpent: 60 }, + ]); + }); +}); diff --git a/01-js/tests/palindrome.test.js b/01-js/tests/palindrome.test.js new file mode 100644 index 00000000..2f4f4639 --- /dev/null +++ b/01-js/tests/palindrome.test.js @@ -0,0 +1,19 @@ +const isPalindrome = require('../medium/palindrome'); + +describe('isPalindrome', () => { + test('returns true for palindromes', () => { + expect(isPalindrome('level')).toBe(true); + expect( + isPalindrome('A man, a plan, a canal. Panama') + ).toBe(true); + expect(isPalindrome('racecar')).toBe(true); + expect(isPalindrome('Nan')).toBe(true); + expect(isPalindrome('')).toBe(true); + }); + + test('returns false for non-palindromes', () => { + expect(isPalindrome('hello')).toBe(false); + expect(isPalindrome('openai')).toBe(false); + expect(isPalindrome('abcde')).toBe(false); + }); +}); diff --git a/01-js/tests/todo-list.test.js b/01-js/tests/todo-list.test.js new file mode 100644 index 00000000..8bcb64fc --- /dev/null +++ b/01-js/tests/todo-list.test.js @@ -0,0 +1,71 @@ +const Todo = require('../hard/todo-list'); + +describe('Todo', () => { + let todoList; + + beforeEach(() => { + todoList = new Todo(); + }); + + test('add and getAll', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + todoList.add('Task 3'); + + expect(todoList.getAll()).toEqual([ + 'Task 1', + 'Task 2', + 'Task 3', + ]); + }); + + test('remove', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + todoList.add('Task 3'); + + todoList.remove(1); + expect(todoList.getAll()).toEqual(['Task 1', 'Task 3']); + + todoList.remove(0); + expect(todoList.getAll()).toEqual(['Task 3']); + + todoList.remove(2); + expect(todoList.getAll()).toEqual(['Task 3']); + }); + + test('update', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + todoList.add('Task 3'); + + todoList.update(1, 'Updated Task 2'); + expect(todoList.get(1)).toBe('Updated Task 2'); + + todoList.update(3, 'Invalid Task'); + expect(todoList.getAll()).toEqual([ + 'Task 1', + 'Updated Task 2', + 'Task 3', + ]); + }); + + test('get', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + todoList.add('Task 3'); + + expect(todoList.get(0)).toBe('Task 1'); + expect(todoList.get(2)).toBe('Task 3'); + expect(todoList.get(3)).toBeNull(); + }); + + test('clear', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + todoList.add('Task 3'); + + todoList.clear(); + expect(todoList.getAll()).toEqual([]); + }); +}); From 31dae4e159262adfe615ac19bcb7fc1df5ee1150 Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Mon, 12 Jun 2023 13:53:22 +0530 Subject: [PATCH 2/8] added more test cases for easy problems --- 01-js/tests/anagram.test.js | 19 ++++++++++++ 01-js/tests/expenditure-analysis.test.js | 38 ++++++++++++------------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/01-js/tests/anagram.test.js b/01-js/tests/anagram.test.js index fbb56cdf..78797d70 100644 --- a/01-js/tests/anagram.test.js +++ b/01-js/tests/anagram.test.js @@ -16,4 +16,23 @@ describe('isAnagram', () => { expect(isAnagram('hello', 'lhel')).toBe(false); expect(isAnagram('working', 'non')).toBe(false); }); + + test('returns true for anagrams with different casing', () => { + expect(isAnagram('Debit Card', 'Bad Credit')).toBe( + true + ); + expect( + isAnagram('School MASTER', 'The ClassROOMs') + ).toBe(true); + }); + + test('returns true for anagrams with special characters', () => { + expect(isAnagram('abc!', '!bac')).toBe(true); + expect(isAnagram('cinema', 'iceman!')).toBe(true); + }); + + test('returns false for non-anagrams with special characters', () => { + expect(isAnagram('hello', 'hello!')).toBe(false); + expect(isAnagram('openai!', 'open')).toBe(false); + }); }); diff --git a/01-js/tests/expenditure-analysis.test.js b/01-js/tests/expenditure-analysis.test.js index 94eb0cd7..8c3599c6 100644 --- a/01-js/tests/expenditure-analysis.test.js +++ b/01-js/tests/expenditure-analysis.test.js @@ -1,6 +1,25 @@ const calculateTotalSpentByCategory = require('../easy/expenditure-analysis'); describe('calculateTotalSpentByCategory', () => { + test('returns the correct total spent for a single transaction', () => { + const transactions = [ + { + id: 1, + timestamp: 1656076800000, + price: 10, + category: 'Food', + itemName: 'Pizza', + }, + ]; + + const result = + calculateTotalSpentByCategory(transactions); + + expect(result).toEqual([ + { category: 'Food', totalSpent: 10 }, + ]); + }); + test('returns the correct total spent for each category', () => { const transactions = [ { @@ -57,25 +76,6 @@ describe('calculateTotalSpentByCategory', () => { expect(result).toEqual([]); }); - test('returns the correct total spent for a single transaction', () => { - const transactions = [ - { - id: 1, - timestamp: 1656076800000, - price: 10, - category: 'Food', - itemName: 'Pizza', - }, - ]; - - const result = - calculateTotalSpentByCategory(transactions); - - expect(result).toEqual([ - { category: 'Food', totalSpent: 10 }, - ]); - }); - test('returns the correct total spent when multiple transactions have the same category', () => { const transactions = [ { From e8fb18bc60359e614cc24f2ef0d24a786e80cd37 Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Mon, 12 Jun 2023 14:17:31 +0530 Subject: [PATCH 3/8] added additional tests for medium and hard problems --- 01-js/tests/calculator.test.js | 110 ++++++++++++++++++++++----------- 01-js/tests/palindrome.test.js | 50 +++++++++++++-- 01-js/tests/todo-list.test.js | 25 ++++++++ 3 files changed, 143 insertions(+), 42 deletions(-) diff --git a/01-js/tests/calculator.test.js b/01-js/tests/calculator.test.js index dc3f5fd4..1cdfc0a8 100644 --- a/01-js/tests/calculator.test.js +++ b/01-js/tests/calculator.test.js @@ -52,42 +52,78 @@ describe('Calculator', () => { expect(calc.getResult()).toBe(0); }); - describe('calculate expression method tests suite', () => { - test('calculate addition and multiplication', () => { - calc.calculate('2 + 3 * 4'); - expect(calc.getResult()).toBe(14); - }); - - test('calculate division', () => { - calc.calculate('( 15 + 3) / 6 '); - expect(calc.getResult()).toBe(3); - }); - - test('calculate subtraction', () => { - calc.calculate('10 - (4 + 2)'); - expect(calc.getResult()).toBe(4); - }); - - test('calculate complex expression', () => { - calc.calculate( - '10 + 2 * ( 6 - (4 + 1) / 2) + 7' - ); - expect(calc.getResult()).toBe(24); - }); - - test('calculate expression with decimals', () => { - calc.calculate('(2.5 + 1.5) * 3'); - expect(calc.getResult()).toBe(12); - }); - - test('calculate expression with invalid characters', () => { - expect(() => calc.calculate('5 + abc')).toThrow( - Error - ); - }); - - test('calculate division by zero', () => { - expect(() => calc.calculate('10 / 0')).toThrow(Error); - }); + test('calculate addition and multiplication', () => { + calc.calculate('2 + 3 * 4'); + expect(calc.getResult()).toBe(14); + }); + + test('calculate division in expression', () => { + calc.calculate('( 15 + 3) / 6 '); + expect(calc.getResult()).toBe(3); + }); + + test('calculate subtraction in expression', () => { + calc.calculate('10 - (4 + 2)'); + expect(calc.getResult()).toBe(4); + }); + + test('calculate complex expression', () => { + calc.calculate('(2 + 3) * (6 - (4 + 1) / 2) + 7'); + expect(calc.getResult()).toBe(24); + }); + test('calculate complex expression with spaces', () => { + calc.calculate( + '10 + 2 * ( 6 - (4 + 1) / 2) + 7' + ); + expect(calc.getResult()).toBe(24); + }); + + test('calculate expression with decimals', () => { + calc.calculate('(2.5 + 1.5) * 3'); + expect(calc.getResult()).toBe(12); + }); + + test('calculate expression with invalid characters', () => { + expect(() => calc.calculate('5 + abc')).toThrow(Error); + expect(() => + calc.calculate('10 * (2 + 3) + xyz') + ).toThrow(Error); + }); + + test('calculate division by zero', () => { + expect(() => calc.calculate('10 / 0')).toThrow(Error); + }); + + test('multiplication with negative numbers', () => { + calc.add(-5); + calc.multiply(-3); + expect(calc.getResult()).toBe(15); + + calc.multiply(0); + expect(calc.getResult()).toBe(0); + }); + + test('division with decimal numbers', () => { + calc.add(10); + calc.divide(3); + expect(calc.getResult()).toBeCloseTo(3.333333, 6); + + calc.divide(2); + expect(calc.getResult()).toBeCloseTo(1.666666, 6); + }); + + test('chained arithmetic operations', () => { + calc.add(10).subtract(5).multiply(2).divide(3); + expect(calc.getResult()).toBeCloseTo(5.0, 6); + }); + + test('expression with invalid parentheses', () => { + expect(() => calc.calculate('10 + (2 + 3')).toThrow( + Error + ); + expect(() => calc.calculate('10 + 2) + 3')).toThrow( + Error + ); + expect(() => calc.calculate(')10 + 2(')).toThrow(Error); }); }); diff --git a/01-js/tests/palindrome.test.js b/01-js/tests/palindrome.test.js index 2f4f4639..3027ab41 100644 --- a/01-js/tests/palindrome.test.js +++ b/01-js/tests/palindrome.test.js @@ -3,12 +3,7 @@ const isPalindrome = require('../medium/palindrome'); describe('isPalindrome', () => { test('returns true for palindromes', () => { expect(isPalindrome('level')).toBe(true); - expect( - isPalindrome('A man, a plan, a canal. Panama') - ).toBe(true); expect(isPalindrome('racecar')).toBe(true); - expect(isPalindrome('Nan')).toBe(true); - expect(isPalindrome('')).toBe(true); }); test('returns false for non-palindromes', () => { @@ -16,4 +11,49 @@ describe('isPalindrome', () => { expect(isPalindrome('openai')).toBe(false); expect(isPalindrome('abcde')).toBe(false); }); + + test('returns true for an empty string', () => { + expect(isPalindrome('')).toBe(true); + }); + + test('handles case-insensitivity correctly', () => { + expect(isPalindrome('Anna')).toBe(true); + expect(isPalindrome('aNnA')).toBe(true); + expect(isPalindrome('Madam')).toBe(true); + expect(isPalindrome('MaDaM')).toBe(true); + expect(isPalindrome('RaCeCaR')).toBe(true); + expect(isPalindrome('rAcEcAr')).toBe(true); + }); + + test('returns true for single-character strings', () => { + expect(isPalindrome('a')).toBe(true); + expect(isPalindrome('z')).toBe(true); + expect(isPalindrome('5')).toBe(true); + expect(isPalindrome('@')).toBe(true); + }); + + test('returns true for strings with spaces', () => { + expect(isPalindrome('race car')).toBe(true); + expect( + isPalindrome('A man a plan a canal Panama') + ).toBe(true); + expect( + isPalindrome('Was it a car or a cat I saw') + ).toBe(true); + }); + + test('returns true for strings with punctuation marks', () => { + expect( + isPalindrome('Able, was I ere I saw Elba!') + ).toBe(true); + expect( + isPalindrome('Eva, can I see bees in a cave?') + ).toBe(true); + expect(isPalindrome('Mr. Owl ate my metal worm.')).toBe( + true + ); + expect( + isPalindrome('A man, a plan, a canal. Panama') + ).toBe(true); + }); }); diff --git a/01-js/tests/todo-list.test.js b/01-js/tests/todo-list.test.js index 8bcb64fc..4ff2c466 100644 --- a/01-js/tests/todo-list.test.js +++ b/01-js/tests/todo-list.test.js @@ -68,4 +68,29 @@ describe('Todo', () => { todoList.clear(); expect(todoList.getAll()).toEqual([]); }); + + test('remove and update with invalid indexes', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + + todoList.remove(5); + expect(todoList.getAll()).toEqual(['Task 1', 'Task 2']); + + todoList.update(3, 'Updated Task'); + expect(todoList.getAll()).toEqual(['Task 1', 'Task 2']); + }); + + test('add duplicate tasks', () => { + todoList.add('Task 1'); + todoList.add('Task 2'); + todoList.add('Task 1'); + todoList.add('Task 3'); + + expect(todoList.getAll()).toEqual([ + 'Task 1', + 'Task 2', + 'Task 1', + 'Task 3', + ]); + }); }); From 9236d46f448f5727b675bc24bf46aa47ce086e1b Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Mon, 12 Jun 2023 15:19:16 +0530 Subject: [PATCH 4/8] fixing bugs in anagram tests --- 01-js/tests/anagram.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/01-js/tests/anagram.test.js b/01-js/tests/anagram.test.js index 78797d70..a66dcac5 100644 --- a/01-js/tests/anagram.test.js +++ b/01-js/tests/anagram.test.js @@ -22,13 +22,12 @@ describe('isAnagram', () => { true ); expect( - isAnagram('School MASTER', 'The ClassROOMs') + isAnagram('School MASTER', 'The ClassROOM') ).toBe(true); }); test('returns true for anagrams with special characters', () => { expect(isAnagram('abc!', '!bac')).toBe(true); - expect(isAnagram('cinema', 'iceman!')).toBe(true); }); test('returns false for non-anagrams with special characters', () => { From 9618c5d61c8c5e0ea6d51309aeabbf289850d8d7 Mon Sep 17 00:00:00 2001 From: Dhruv Sharma Date: Mon, 12 Jun 2023 15:31:43 +0530 Subject: [PATCH 5/8] fixed bugs in calculator tests --- 01-js/tests/calculator.test.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/01-js/tests/calculator.test.js b/01-js/tests/calculator.test.js index 1cdfc0a8..aae39e12 100644 --- a/01-js/tests/calculator.test.js +++ b/01-js/tests/calculator.test.js @@ -69,7 +69,7 @@ describe('Calculator', () => { test('calculate complex expression', () => { calc.calculate('(2 + 3) * (6 - (4 + 1) / 2) + 7'); - expect(calc.getResult()).toBe(24); + expect(calc.getResult()).toBe(24.5); }); test('calculate complex expression with spaces', () => { calc.calculate( @@ -106,15 +106,10 @@ describe('Calculator', () => { test('division with decimal numbers', () => { calc.add(10); calc.divide(3); - expect(calc.getResult()).toBeCloseTo(3.333333, 6); + expect(calc.getResult()).toBeCloseTo(3.333333, 2); calc.divide(2); - expect(calc.getResult()).toBeCloseTo(1.666666, 6); - }); - - test('chained arithmetic operations', () => { - calc.add(10).subtract(5).multiply(2).divide(3); - expect(calc.getResult()).toBeCloseTo(5.0, 6); + expect(calc.getResult()).toBeCloseTo(1.666666, 2); }); test('expression with invalid parentheses', () => { From fd8197758ef4ccaab699b7cbf8ed9bccca181f65 Mon Sep 17 00:00:00 2001 From: cataschrodinger Date: Mon, 1 Jan 2024 13:36:58 +0530 Subject: [PATCH 6/8] Easy problems solved & all tests run successfully --- 01-js/easy/anagram.js | 15 ++++++++-- 01-js/easy/expenditure-analysis.js | 47 +++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index fff61427..e5012741 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -6,9 +6,20 @@ Once you've implemented the logic, test your code by running - `npm run test-anagram` */ +function sortString(str){ + const upperCased = str.toUpperCase(); + const arrString = upperCased.split(''); + const sortedArray = arrString.sort(); + const ans = sortedArray.join(''); + return ans; +} function isAnagram(str1, str2) { - + if(sortString(str1)==sortString(str2)){ + return true; + } + return false; } - +// console.log(sortString('Debit Card')); +// console.log(sortString('Bad Credit')); module.exports = isAnagram; diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 20fbb943..8fbdddb7 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -9,7 +9,52 @@ */ function calculateTotalSpentByCategory(transactions) { - return []; + let listOfcategory = []; + let listOfPrices = []; + for(let i=0; i { + const keyValue = obj[commonKey]; + + if (resultMap.hasOwnProperty(keyValue)) { + // If the key already exists, add the value of the sumKey + resultMap[keyValue][sumKey] += obj[sumKey]; + } else { + // If the key doesn't exist, create a new entry in the resultMap + resultMap[keyValue] = { [commonKey]: keyValue, [sumKey]: obj[sumKey] }; + } + }); + + // Convert the resultMap object to an array of values + const resultArray = Object.values(resultMap); + + return resultArray; + } + + const combinedList = combineObjectsWithSameKey(resultList, "category", "totalSpent"); + + return combinedList; } module.exports = calculateTotalSpentByCategory; + + +// MY SOLUTION +// 1. syntax of list & objects +// 2. To make 2 list of categories & prices +// 3. to convert the two lists to one list of object +// 4. Map function from chat gpt to combine the objects to unique one and add sum \ No newline at end of file From b3d7a68486a123505746462e5faa6043b7177056 Mon Sep 17 00:00:00 2001 From: cataschrodinger Date: Tue, 2 Jan 2024 02:14:13 +0530 Subject: [PATCH 7/8] 01 all assignments done --- 01-js/hard/calculator.js | 49 +++++++++++++++++++++++++++++++++++++- 01-js/hard/todo-list.js | 36 ++++++++++++++++++++++++++-- 01-js/medium/palindrome.js | 27 +++++++++++++++++++-- 01-js/medium/times.js | 13 ++++++++-- 4 files changed, 118 insertions(+), 7 deletions(-) diff --git a/01-js/hard/calculator.js b/01-js/hard/calculator.js index 82d48229..0c3503d2 100644 --- a/01-js/hard/calculator.js +++ b/01-js/hard/calculator.js @@ -17,6 +17,53 @@ - `npm run test-calculator` */ -class Calculator {} +class Calculator { + constructor(){ + this.result = 0; + } + clear(){ + this.result = 0; + } + getResult(){ + return this.result; + } + add(num){ + this.result+=num; + } + subtract(num){ + this.result-=num; + } + multiply(num){ + if(num == 0){ + this.result = 0; + }else{ + this.result*=num; + } + + } + divide(num){ + if(num == 0){ + throw new Error; + }else{ + this.result/=num; + } + } + calculate(str){ + try { + this.result = eval(str); + if(this.result == Infinity || this.result == -Infinity){ + throw new Error; + } + } catch (error) { + throw new Error; + } + } +} module.exports = Calculator; +// let calc = new Calculator(); +// calc.subtract(5); +// calc.add(-100); +// calc.multiply(10); +// calc.divide(0); +// console.log(calc.getResult()); \ No newline at end of file diff --git a/01-js/hard/todo-list.js b/01-js/hard/todo-list.js index 7c9c1806..1a12ad95 100644 --- a/01-js/hard/todo-list.js +++ b/01-js/hard/todo-list.js @@ -12,7 +12,39 @@ */ class Todo { - + constructor(){ + this.list = []; + } + getAll(){ + return this.list; + } + get(i){ + if(i>=this.list.length){ + return null; + } + return this.list[i]; + } + clear(){ + this.list = []; + } + add(str){ + this.list.push(str); + } + remove(i){ + this.list.splice(i, 1); + } + update(i, str){ + if(i>=this.list.length){ + + }else{this.list[i] = str;} + + } } - +// let doIt = new Todo(); +// doIt.add('Task 1'); +// doIt.add('Task 2'); +// doIt.add('Task 3'); +// doIt.update(1, 'Updated Task 2') +// console.log(doIt.get(3)); module.exports = Todo; + diff --git a/01-js/medium/palindrome.js b/01-js/medium/palindrome.js index d8fe2d8f..74ba8f2d 100644 --- a/01-js/medium/palindrome.js +++ b/01-js/medium/palindrome.js @@ -6,8 +6,31 @@ - `npm run test-palindrome` */ -function isPalindrome(str) { - return true; +function removePunctuationAndSpaces(inputString) { + // Use a regular expression to match punctuation and spaces /g is for global and \s is space + const regex = /[.?,\/#!$%\^&\*;:{}=\-_`~()\s]/g; + + // Replace the matched characters with an empty string + const resultString = inputString.replace(regex, ''); + + return resultString; } +function isPalindrome(str) { + const str1 = removePunctuationAndSpaces(str); + const lowerStr = str1.toLowerCase(); + const strArray = lowerStr.split(''); + + let revArray = []; + for(let i=0; i Date: Thu, 4 Jan 2024 23:55:38 +0530 Subject: [PATCH 8/8] Few easy solved, pushing just for the sake of pushing --- 02-async-js/easy/counter.js | 9 +++++++++ 02-async-js/easy/counter_without_setinterval.js | 10 ++++++++++ 02-async-js/easy/file.txt | 1 + 02-async-js/easy/read_from_file.js | 6 ++++++ 4 files changed, 26 insertions(+) create mode 100644 02-async-js/easy/counter.js create mode 100644 02-async-js/easy/counter_without_setinterval.js create mode 100644 02-async-js/easy/file.txt create mode 100644 02-async-js/easy/read_from_file.js diff --git a/02-async-js/easy/counter.js b/02-async-js/easy/counter.js new file mode 100644 index 00000000..ca74310f --- /dev/null +++ b/02-async-js/easy/counter.js @@ -0,0 +1,9 @@ +function counter(){ + let count = 0; + setInterval(()=>{ + console.log(count); + count++; + }, 1000); +}; + +counter(); \ No newline at end of file diff --git a/02-async-js/easy/counter_without_setinterval.js b/02-async-js/easy/counter_without_setinterval.js new file mode 100644 index 00000000..00662e29 --- /dev/null +++ b/02-async-js/easy/counter_without_setinterval.js @@ -0,0 +1,10 @@ +let count =0; +function counter(){ + console.log(count); + count++; + setTimeout(()=>{ + counter(); + },1000) +}; + +counter(); \ No newline at end of file diff --git a/02-async-js/easy/file.txt b/02-async-js/easy/file.txt new file mode 100644 index 00000000..f3e2710d --- /dev/null +++ b/02-async-js/easy/file.txt @@ -0,0 +1 @@ +lol lul \ No newline at end of file diff --git a/02-async-js/easy/read_from_file.js b/02-async-js/easy/read_from_file.js new file mode 100644 index 00000000..a14e807f --- /dev/null +++ b/02-async-js/easy/read_from_file.js @@ -0,0 +1,6 @@ +const fs = require("fs"); + +fs.readFile("file.txt", "utf-8", (err,data)=>{ + if(err) throw err; + console.log(data); +}) \ No newline at end of file