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
1 change: 1 addition & 0 deletions 01-js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 14 additions & 1 deletion 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@
*/

function isAnagram(str1, str2) {

if( str1.length !== str2.length ) return false;
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
const freq = new Array(256).fill(0);
for(let i = 0 ; i < str1.length ; i++){
freq[str1[i].charCodeAt(0)]++;
}
for(let letter of str2){
freq[letter.charCodeAt(0)]--;
}
for(let i = 0 ; i < 256; i++){
if(freq[i] !== 0)return false;
}
return true;
}

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

function calculateTotalSpentByCategory(transactions) {
return [];
const mp = {};
for(let i = 0 ; i < transactions.length; i++){
const currentTransaction = transactions[i];
const category = currentTransaction.category;
const price = currentTransaction.price;
if(!mp[category]){
mp[category] = price;
}
else{
mp[category] += price;
}
}
return Object.entries(mp).map(category => {
const [categoryName , totalMoneySpent] = category;
return {
category: categoryName,
totalSpent: totalMoneySpent,
}
})
// return [];
}

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) {

let largestElement = numbers[0];
for(const num of numbers){
if(num > largestElement)largestElement = num;
}
return largestElement;
}

module.exports = findLargestElement;
26 changes: 25 additions & 1 deletion 01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@
*/

class Todo {

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

module.exports = Todo;
10 changes: 9 additions & 1 deletion 01-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
const isVowel = (letter) => {
const lowerCased = letter.toLowerCase();
return lowerCased === 'a' || lowerCased === 'e' || lowerCased === 'i' || lowerCased === 'o' || lowerCased === 'u'
}
let vowelCount = 0;
for(const char of str){
if(isVowel(char))vowelCount++;
}
return vowelCount;
}

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 newStr = "";
str = str.toLowerCase();
for (const char of str) {
if (
char.charCodeAt(0) >= "a".charCodeAt(0) &&
char.charCodeAt(0) <= "z".charCodeAt(0)
)
newStr += char;
}

let start = 0,
end = newStr.length - 1;
while (start < end) {
if (newStr[start] !== newStr[end]) return false;
start++;
end--;
}
return true;
}

Expand Down
13 changes: 11 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ 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();
for (let i = 1; i <= n; i++);
const end = Date.now();
const time_elapsed = (end - start) ;
return time_elapsed;
}

const time1 = calculateTime(100);
const time2 = calculateTime(100000);
const time3 = calculateTime(1000000000);
console.log(time1, time2, time3);
Loading