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

added anagram.js #355

Open
wants to merge 5 commits into
base: hkirat/with-tests
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
7 changes: 6 additions & 1 deletion 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
*/

function isAnagram(str1, str2) {

if (
str1.toLowerCase().split("").sort().join() ==
str2.toLowerCase().split("").sort().join()
)
return true;
return false;
}

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

function calculateTotalSpentByCategory(transactions) {
return [];
var expenses = {};
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].category in expenses) {
expenses[transactions[i].category] =
expenses[transactions[i].category] + transactions[i].price; //add to existing price of category transactions[i].category
} else {
expenses[transactions[i].category] = transactions[i].price; //add new category with price
}
}
var ans = [];
keys = Object.keys(expenses); //fetch all keys

for (let i = 0; i < keys.length; i++) {
ans.push({ category: keys[i], totalSpent: expenses[keys[i]] });
}
return ans;
}

module.exports = calculateTotalSpentByCategory;
1 change: 1 addition & 0 deletions 01-js/easy/tempCodeRunnerFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = isAnagram;
35 changes: 34 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,39 @@
- `npm run test-calculator`
*/

class Calculator {}
class Calculator {
constructor() {
this.result = 0;
}
add(number) {
return this.result += number;
}

subtract(number) {
return this.result -= number;
}

multiply(number) {
return this.result *= number;
}

divide(number) {
if (number == 0) console.log("Invalid divisor!");
return this.result /= number;
}

clear() {
this.result = 0;
}

getResult() {
return this.result;
}

calculate(string) {

}

}

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

class Todo {
constructor() {
this.arr = []; //initilize with an empty array
}

add(task) {
this.arr.push(task);
}

remove(indexOfTask) {
if (indexOfTask > this.arr.length) {
console.log("Invalid index");
return null;
} else {
for (var i = indexOfTask; i < this.arr.length; i++) {
this.arr[i] = this.arr[i + 1];
}
this.arr.length = this.arr.length - 1;
}
}

update(index, updatedTask) {
if (index > this.arr.length - 1) {
console.log("Invalid index");
return null;
} else {
this.arr[index] = updatedTask;
}
}

getAll() {
return this.arr;
}

get(indexOfTask) {
if (indexOfTask > this.arr.length - 1) {
console.log("invalid index");
return null;
}
return this.arr[indexOfTask];
}

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

}

Expand Down
16 changes: 16 additions & 0 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
*/

function isPalindrome(str) {
if (str.length == 0 || str.length == 1) return true;
let cleanString = "";
for (let i = 0; i < str.length; i++) {
if (
(str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) ||
(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90)
)
cleanString += str[i].toLowerCase();
}
let i = 0,
j = cleanString.length - 1;
while (i < j) {
if (cleanString[i] != cleanString[j]) return false;
i++;
j--;
}
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 @@ -6,7 +6,16 @@ Try running it for
3. Sum from 1-1000000000
Hint - use Date class exposed in JS
*/
function sum(n) {
if (n == 1) return;
let sum = 1;
for (let i = 2; i <= n; i++) {
sum += i;
}
}

function calculateTime(n) {
return 0.01;
}
let start = Date.now();
sum(n);
return Date.now() - start;
}