Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy solved #336

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 01-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
15 changes: 13 additions & 2 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
47 changes: 46 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,52 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
let listOfcategory = [];
let listOfPrices = [];
for(let i=0; i<transactions.length; i++){
listOfcategory[i] = transactions[i]["category"];
listOfPrices[i] = transactions[i]["price"];
}
const resultList = [];
for(let i=0; i<listOfPrices.length; i++){
const obj = {
category: listOfcategory[i],
totalSpent: listOfPrices[i]
};
resultList.push(obj);
}

function combineObjectsWithSameKey(objects, commonKey, sumKey) {
const resultMap = {};

objects.forEach(obj => {
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
49 changes: 48 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
36 changes: 34 additions & 2 deletions 01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

27 changes: 25 additions & 2 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<strArray.length; i++){
revArray[i] = strArray[strArray.length-i-1];
}
const str2 = revArray.join('');
console.log(str2);

if(lowerStr == str2){
return true;
}
return false;
}
module.exports = isPalindrome;
13 changes: 11 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ Hint - use Date class exposed in JS
*/

function calculateTime(n) {
return 0.01;
}
let startTime = new Date();
let sum = 0;
for(let i=1; i<=n; i++){
sum = sum +i;
}
let endTime = new Date();
let timeSec = (endTime-startTime)/1000;
return timeSec;
}

console.log(calculateTime(100000));
21 changes: 21 additions & 0 deletions 01-js/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
37 changes: 37 additions & 0 deletions 01-js/tests/anagram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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);
});

test('returns true for anagrams with different casing', () => {
expect(isAnagram('Debit Card', 'Bad Credit')).toBe(
true
);
expect(
isAnagram('School MASTER', 'The ClassROOM')
).toBe(true);
});

test('returns true for anagrams with special characters', () => {
expect(isAnagram('abc!', '!bac')).toBe(true);
});

test('returns false for non-anagrams with special characters', () => {
expect(isAnagram('hello', 'hello!')).toBe(false);
expect(isAnagram('openai!', 'open')).toBe(false);
});
});
Loading