Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 7 additions & 1 deletion week-2/week-2-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
*/

function isAnagram(str1, str2) {

if(str1.length !== str2.length){
return false;
}
function sortingStr(str){
return str.toLowerCase().split('').sort().join('');
}
return sortingStr(str1) === sortingStr(str2);
}

module.exports = isAnagram;
28 changes: 21 additions & 7 deletions week-2/week-2-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@
transactions is an array where each
Transaction - an object like
{
id: 1,
timestamp: 1656076800000,
price: 10,
category: 'Food',
itemName: 'Pizza',
}
id: 1,
timestamp: 1656076800000,
price: 10,
category: 'Food',
itemName: 'Pizza',
}
Output - [{ category: 'Food', totalSpent: 10 }] // Can have multiple categories, only one example is mentioned here
*/

function calculateTotalSpentByCategory(transactions) {
return [];
const totals = {};
for (let transaction of transactions) {
const { category, price } = transaction;
if (totals[category]) {
totals[category] += price;
}
else {
totals[category] = price;
}
}
const result = [];
for (let category in totals) {
result.push({ category, totalSpent: totals[category] });
}
return result;
}

module.exports = calculateTotalSpentByCategory;
16 changes: 11 additions & 5 deletions week-2/week-2-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/*
  Write a function `findLargestElement` that takes an array of numbers and returns the largest element.
  Example:
  - Input: [3, 7, 2, 9, 1]
  - Output: 9
Write a function `findLargestElement` that takes an array of numbers and returns the largest element.
Example:
- Input: [3, 7, 2, 9, 1]
- Output: 9
*/

function findLargestElement(numbers) {

let max = numbers[0];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}

module.exports = findLargestElement;
46 changes: 45 additions & 1 deletion week-2/week-2-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,50 @@
Once you've implemented the logic, test your code by running
*/

class Calculator {}
class Calculator {
constructor() {
this.result = 0;
}
add(number) {
this.result += number;
}
subtract(number) {
this.result -= number;
}
multiply(number) {
this.result *= number;
}
divide(number) {
if (number == 0) {
throw new Error("Cannot divide by zero");
}
this.result /= number;
}
clear() {
this.result = 0;
}
getResult() {
return this.result;
}

calculate(inputExpression) {
const cleanedExpression = inputExpression.split(' ').join('');
const allowed = '0123456789+-*/().';
for (let ch of cleanedExpression) {
if (!allowed.includes(ch)) {
throw new Error("Invalid Expression");
}
}
try {
this.result = eval(inputExpression);
} catch (error) {
throw new Error("Invalid expression");
}
if (this.result === Infinity) {
throw new Error("Cannot divide by zero");
}
return this.result;
}
}

module.exports = Calculator;
29 changes: 28 additions & 1 deletion week-2/week-2-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,34 @@
*/

class Todo {

constructor() {
this.todos = [];
}
add(todo) {
this.todos.push(todo);
}
remove(indexOfTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todos.length) {
this.todos.splice(indexOfTodo, 1);
}
}
update(index, updatedTodo) {
if (index >= 0 && index < this.todos.length) {
this.todos[index] = updatedTodo;
}
}
getAll() {
return this.todos;
}
get(indexOfTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todos.length) {
return this.todos[indexOfTodo];
}
return null;
}
clear() {
this.todos = [];
}
}

module.exports = Todo;
10 changes: 9 additions & 1 deletion week-2/week-2-js/medium/countVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
*/

function countVowels(str) {
// Your code here
// Your code here
let count = 0;
const vowels = 'aeiouAEIOU';
for (let s of str) {
if (vowels.includes(s)) {
count++;
}
}
return count;
}

module.exports = countVowels;
17 changes: 17 additions & 0 deletions week-2/week-2-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
*/

function isPalindrome(str) {
let lowerStr = '';
for (let ch of str) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
lowerStr += ch.toLowerCase();
} else if (ch >= '0' && ch <= '9') {
lowerStr += ch;
}
}
let left = 0;
let right = lowerStr.length - 1;
while (left < right) {
if (lowerStr[left] !== lowerStr[right]) {
return false;
}
left++;
right--;
}
return true;
}

Expand Down
14 changes: 12 additions & 2 deletions week-2/week-2-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ There is no automated test for this one, this is more for you to understand time
*/

function calculateTime(n) {
return 0.01;
}
const startTime = performance.now();
let sum = 0;
for(let i = 1;i<=n ;i++){
sum+= i;
}
const endTime = performance.now();
return (endTime-startTime)/1000;
}

console.log('Time for sum from 1 to 100:', calculateTime(100));
console.log('Time for sum from 1 to 100000:', calculateTime(100000));
console.log('Time for sum from 1 to 1000000000:', calculateTime(1000000000));
16 changes: 16 additions & 0 deletions week-2/week-2-js/node_modules/.bin/browserslist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions week-2/week-2-js/node_modules/.bin/browserslist.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions week-2/week-2-js/node_modules/.bin/browserslist.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions week-2/week-2-js/node_modules/.bin/create-jest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions week-2/week-2-js/node_modules/.bin/create-jest.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions week-2/week-2-js/node_modules/.bin/create-jest.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions week-2/week-2-js/node_modules/.bin/esparse

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions week-2/week-2-js/node_modules/.bin/esparse.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading