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

expenditure problem solved #347

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
6 changes: 6 additions & 0 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
*/

function isAnagram(str1, str2) {

let a = str1.toLowerCase().split("").sort().join("")
let b = str2.toLowerCase().split("").sort().join("")

return (a===b ? true:false)

}


module.exports = isAnagram;
55 changes: 53 additions & 2 deletions 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,59 @@
- `npm run test-expenditure-analysis`
*/

const currentDate = new Date()
function calculateTotalSpentByCategory(transactions) {
return [];
const categoryItems = {}
transactions.forEach((transaction)=>{
const category = transaction.category
if(!categoryItems[category]){
categoryItems[category] = 0
}
categoryItems[category] += transaction.price
})
return [categoryItems];
}

module.exports = calculateTotalSpentByCategory;
var transactions = [
{
itemName:'T-shirt',
category:'Fashion',
price:300,
timestamp:currentDate.getTime()
},
{
itemName:'Jeans',
category:'Fashion',
price:700,
timestamp:currentDate.getTime()
},
{
itemName:'Mobile',
category:'Gadgets',
price:10000,
timestamp:currentDate.getTime()
},
{
itemName:'Laptop',
category:'Gadgets',
price:50000,
timestamp:currentDate.getTime()
},
{
itemName:'Biriyani',
category:'Food',
price:200,
timestamp:currentDate.getTime()
},
{
itemName:'Chiken',
category:'Food',
price:150,
timestamp:currentDate.getTime()
},

]

console.log(calculateTotalSpentByCategory(transactions))

// module.exports = calculateTotalSpentByCategory;
23 changes: 23 additions & 0 deletions 01-js/easy/test-anagram.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const isAnagram = require('./anagram');

describe('isAnagram', () => {
it('should return true for anagrams', () => {
expect(isAnagram('lion', 'ionl')).toBe(true);
expect(isAnagram('listen', 'silent')).toBe(true);
expect(isAnagram('acts', 'cats')).toBe(true);
});

it('should return false for non-anagrams', () => {
expect(isAnagram('hello', 'world')).toBe(false);
expect(isAnagram('apple', 'banana')).toBe(false);
expect(isAnagram('abc', 'abcd')).toBe(false);
});

it('should be case-insensitive', () => {
expect(isAnagram('LiOn', 'IoNl')).toBe(true);
});

it('should work with empty strings', () => {
expect(isAnagram('', '')).toBe(true);
});
});
7 changes: 6 additions & 1 deletion 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
*/

function isPalindrome(str) {
return true;
const reverseStr = str.toLowerCase().split('').reverse().join('');
if (str.toLowerCase() === reverseStr) {
return true;
}
}

console.log(isPalindrome("Nayan"))

module.exports = isPalindrome;
15 changes: 13 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ Try running it for
Hint - use Date class exposed in JS
*/

const s = new Date();
function calculateTime(n) {
return 0.01;
}
let start = new Date().getTime();
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
// console.log(sum);
let end = new Date().getTime();
let time = (end - start) / 1000;
return time;
}

console.log(calculateTime(100000))
Loading