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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@

function isAnagram(str1, str2) {

if(str1.length !== str2.length) return false;

const countAnagram = {};

for(const char of str1) {
const str = char.toLowerCase();
if(countAnagram[str]) {
countAnagram[str]++;
}else{
countAnagram[str] = 1;
}
}

for(const char of str2) {
const str = char.toLowerCase();
if(countAnagram[str]) {
if(countAnagram[str] === 1) {
delete countAnagram[str];
}else {
countAnagram[str]--;
}
}
}

return Object.keys(countAnagram).length === 0;
}

module.exports = isAnagram;
15 changes: 14 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
const result = transactions.reduce((acc, item) => {
if(acc[item.category]) {
acc[item.category] = acc[[item.category]] + item.price;
}else{
acc[item.category] = item.price;
}
return acc;
}, {});

return Object.keys(result).map((item) => ({
category: item,
totalSpent: result[item]
}))

}

module.exports = calculateTotalSpentByCategory;
6 changes: 5 additions & 1 deletion 01-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/

function findLargestElement(numbers) {

if(!numbers.length) return undefined;
return numbers.reduce((acc, item) => {
if(item > acc) acc = item;
return acc;
}, Number.NEGATIVE_INFINITY)
}

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

class Calculator {}
class Calculator {
result;

constructor() {
this.result = 0;
}

add(num) {
this.result += num;
}

sub(num) {
this.result -= num;
}

multiply(num) {
this.result *= num;
}

divide(num) {
this.result /= num;
}

clear() {
this.result = 0;
}

getResult() {
return this.result;
}

calculate(exp) {
const tokens = exp.split(" ");
const stack = [];
const precedence = {
"+": 1,
"-": 1,
"*": 2,
"/": 2,
};

for (const token of tokens) {
if (!isNaN(token)) {
stack.push(Number(token));
} else if (token in precedence) {
while (
stack.length > 0 &&
precedence[stack[stack.length - 1]] >= precedence[token]
) {
const op = stack.pop();
const b = stack.pop();
const a = stack.pop();
switch (op) {
case "+":
stack.push(a + b);
break;
case "-":
stack.push(a - b);
break;
case "*":
stack.push(a * b);
break;
case "/":
stack.push(a / b);
break;
}
}
stack.push(token);
} else if (token === "(") {
stack.push(token);
} else if (token === ")") {
while (stack.length > 0 && stack[stack.length - 1] !== "(") {
const op = stack.pop();
const b = stack.pop();
const a = stack.pop();
switch (op) {
case "+":
stack.push(a + b);
break;
case "-":
stack.push(a - b);
break;
case "*":
stack.push(a * b);
break;
case "/":
stack.push(a / b);
break;
}
}
stack.pop();
} else {
throw new Error("Invalid character: " + token);
}
}
while (stack.length > 0) {
const op = stack.pop();
const b = stack.pop();
const a = stack.pop();
switch (op) {
case "+":
stack.push(a + b);
break;
case "-":
stack.push(a - b);
break;
case "*":
stack.push(a * b);
break;
case "/":
stack.push(a / b);
break;
}
}
return stack[0];
}

}


const calc = new Calculator();
calc.add(10);
console.log(calc.getResult());
calc.sub(2);
console.log(calc.getResult());
calc.multiply(6);
console.log(calc.getResult());
calc.divide(2);
console.log(calc.getResult());
calc.clear();
console.log(calc.getResult());



module.exports = Calculator;
54 changes: 54 additions & 0 deletions 01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,60 @@

class Todo {

todos;
constructor(){
this.todos = [];
}

add(todo) {
this.todos.push(todo);
}

remove(index) {
const todos = this.todos.filter((i, idx) => idx !== index);
this.todos = todos;
}

update(idx, updatedTodo) {
const todos = this.todos.map((item, index) => {
if(index === idx) {
return {
...updatedTodo
}
}
return item;
})
this.todos = todos;
}

getAll() {
return this.todos;
}

get(index) {
return this.todos.find((i, idx) => idx === index);
}

clear() {
this.todos = [];
}
}


const todos = new Todo();
todos.add({id:1, name:'item 1'});
todos.add({id:1, name:'item 3'});
todos.add({id:1, name:'item 2'});
todos.add({id:1, name:'item 4'});
todos.add({id:1, name:'item 5'});
console.log(todos.getAll());
console.log(todos.get(3));
todos.update(1, {id: 2, name: 'item 2'})
todos.remove(3)
todos.clear()
todos.add({id:1, name:'item 5'});
console.log(todos.getAll());



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

function countVowels(str) {
// Your code here
let count = 0;
for(const char of str) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
if(vowels.includes(char.toLowerCase())) count++;
}
return count;
}

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

function isPalindrome(str) {

let newString = '';
for(const s of str) {
const ch = s.toLowerCase();
if((ch >= 'a' && ch <= 'z')) {
newString += ch;
}
}

let i = 0, j = newString.length - 1;
while(i <= j) {
const ch1 = newString.charAt(i);
const ch2 = newString.charAt(j);
if(ch1 !== ch2) return false;
i++;
j--;
}
return true;
}

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

function calculateTime(n) {
return 0.01;
}
const start = Date.now();
let sum = 0;
for(let i = 1; i <= n; i++) {
sum += i;
}
const end = Date.now();
const timeInSeconds = (end - start) / 1000;
console.log(`Total time = ${timeInSeconds} seconds`);
return sum;
}

console.log(calculateTime(100));
console.log(calculateTime(100000));
console.log(calculateTime(1000000000));