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

Week 1 assignment #270

Open
wants to merge 5 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
20 changes: 20 additions & 0 deletions week-1/Week-1-assignment-with-tests/01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@
*/

function isAnagram(str1, str2) {
///check if both the strings are of same length
///order it and then use == operator

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

///There is no direct method to order so,
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();


///split the string into array,sort the array then join the array
str1 = str1.split('').sort().join('');
str2 = str2.split('').sort().join('');

if (str1 == str2) {
return true;
} else {
return false;
}
}

module.exports = isAnagram;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,59 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
}
// var transactions = [
// {
// id: 1,
// timestamp: 1656076800000,
// price: 10,
// category: 'Food',
// itemName: 'Pizza',
// },
// {
// id: 2,
// timestamp: 1656259600000,
// price: 20,
// category: 'Food',
// itemName: 'Burger',
// },
// {
// id: 3,
// timestamp: 1656019200000,
// price: 15,
// category: 'Clothing',
// itemName: 'T-Shirt',
// },
// {
// id: 4,
// timestamp: 1656364800000,
// price: 30,
// category: 'Electronics',
// itemName: 'Headphones',
// },
// {
// id: 5,
// timestamp: 1656105600000,
// price: 25,
// category: 'Clothing',
// itemName: 'Jeans',
// },
// ];
///[returnElement] contains{category: 'Food', totalSpent: 30}
var returnElement = [];

transactions.forEach((traEle) => {
///go through each and every element in transactions
var elemen = returnElement.find((ele) => (ele.category === traEle.category));
///check if that element is present
if (elemen) {
elemen.totalSpent = elemen.totalSpent + traEle.price;
returnElement.push({ category: traEle.category, totalSpent: traEle.price });
}
});



return returnElement;
}
// calculateTotalSpentByCategory();
module.exports = calculateTotalSpentByCategory;
36 changes: 35 additions & 1 deletion week-1/Week-1-assignment-with-tests/01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@
- `npm run test-calculator`
*/

class Calculator {}
class Calculator {
constructor() {
this.result = 0;
}
add(addVar) {
this.result = this.result + addVar;
}
subtract(addVar) {
this.result = this.result - addVar;
}
multiply(addVar) {
this.result = this.result * addVar;
}
divide(addVar) {
if (addVar <= 0) {
throw Error;
} else {

this.result = this.result / addVar;
}
}
clear() {
this.result = 0;
}
getResult() {
// console.log(this.result);
return this.result;
}
}
var calc = new Calculator();
// calc.add(12);

// calc.divide(4);
// calc.getResult();
// calc.divide(0);

module.exports = Calculator;
43 changes: 43 additions & 0 deletions week-1/Week-1-assignment-with-tests/01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,50 @@
*/

class Todo {
constructor() {
this.todoList = [];
}
add(addEle) {
this.todoList = [...this.todoList, addEle];
}
remove(popInIndex) {
// if (popInIndex >= this.todoList.length) {
// throw (Error);
// } else {
this.todoList = this.todoList.slice(0, popInIndex).concat(this.todoList.slice(popInIndex + 1, this.todoList.length));
// }
}

update(popInIndex, ele) {
// if (popInIndex < 0 || popInIndex >= this.todoList.length) {
// throw (Error);
// } else {

this.todoList[popInIndex] = ele;

// }
}

getAll() {
return this.todoList;
}

get(indexOfTodo) {
// if (popInIndex < 0 || popInIndex >= this.todoList.length) {
// throw (Error);
// } else {
return this.todoList[indexOfTodo + 1];



// }

}
clear() {
this.todoList = [];
return this.todoList;

}
}

module.exports = Todo;
21 changes: 20 additions & 1 deletion week-1/Week-1-assignment-with-tests/01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@
*/

function isPalindrome(str) {
return true;
var isPali = true;


for (var i = 0; i < (str.length) / 2; i++) {
if (str[i].toLowerCase() != str[str.length - 1 - i].toLowerCase()) {
isPali = false;

}
}
if (isPali) {

return true;


} else {
isPali = true;

return false;
}

}

module.exports = isPalindrome;
15 changes: 13 additions & 2 deletions week-1/Week-1-assignment-with-tests/01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ Hint - use Date class exposed in JS
*/

function calculateTime(n) {
return 0.01;
}
var initialSum = 0;
///will get start time
const startTime = new Date().getTime();
///lets go through all the elements
for (var initial = 0; initial < n; initial++) {
initialSum = initialSum + initial;
}
///here will get end time
const endTime = new Date().getTime();
console.log(initialSum);
return endTime - startTime;

}
2 changes: 1 addition & 1 deletion week-1/Week-1-assignment-with-tests/01-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"keywords": [],
"author": "",
"license": "ISC"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// ## Create a counter in JavaScript

// We have already covered this in the second lesson, but as an easy recap try to code a counter in Javascript
// It should go up as time goes by in intervals of 1 second

///Here the word "Hello" will be printed every 1 seconds
setInterval(() => {
console.log("Hello")
}, 1000);

This file was deleted.

81 changes: 81 additions & 0 deletions week-1/Week-1-assignment-with-tests/02-async-js/easy/2-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// ## Counter without setInterval

// Without using setInterval, try to code a counter in Javascript. There is a hint at the bottom of the file if you get stuck.


///Here we are not using setInterval but setTimeout ,it will run after 1 seconds

function counter() {
console.log("Hello");
setTimeout(counter, 1000);
}
counter()




































































// (Hint: setTimeout)
Loading