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

Solved Week 2 assignments #110

Open
wants to merge 1 commit 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions week-2/week-2-async-js/easy/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let count = 1;
/*
setInterval(() => {
console.log(count);
count++;
}, 1000);
*/

function counter() {
console.log(count);
count++;
setTimeout(counter, 1000);
}

counter();
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/

function wait(n) {
return new Promise((resolve, reject) => {
setTimeout(resolve, n * 1000);
});
}

module.exports = wait;
5 changes: 5 additions & 0 deletions week-2/week-2-async-js/hard (promises)/2-sleep-completely.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
*/

function sleep(milliseconds) {
return new Promise((resolve, reject) => {
const startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliseconds);
resolve();
});
}

module.exports = sleep;
18 changes: 14 additions & 4 deletions week-2/week-2-async-js/hard (promises)/3-promise-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@
*/

function wait1(t) {

return new Promise((resolve, reject) => {
setTimeout(resolve, t * 1000);
});
}

function wait2(t) {

return new Promise((resolve, reject) => {
setTimeout(resolve, t * 1000);
});
}

function wait3(t) {

return new Promise((resolve, reject) => {
setTimeout(resolve, t * 1000);
});
}

function calculateTime(t1, t2, t3) {

const startTime = Date.now();
return Promise.all([wait1(t1), wait2(t2), wait3(t3)]).then(() => {
const endTime = Date.now();
return endTime - startTime;
});
}

module.exports = calculateTime;
22 changes: 18 additions & 4 deletions week-2/week-2-async-js/hard (promises)/4-promise-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,33 @@
*/

function wait1(t) {

return new Promise((resolve, reject) => {
setTimeout(resolve, t * 1000);
});
}

function wait2(t) {

return new Promise((resolve, reject) => {
setTimeout(resolve, t * 1000);
});
}

function wait3(t) {

return new Promise((resolve, reject) => {
setTimeout(resolve, t * 1000);
});
}

function calculateTime(t1, t2, t3) {

const startTime = Date.now();

return wait1(t1)
.then(() => wait2(t2))
.then(() => wait3(t3))
.then(() => {
const endTime = Date.now();
return endTime - startTime;
});
}

module.exports = calculateTime;
4 changes: 4 additions & 0 deletions week-2/week-2-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/

function isAnagram(str1, str2) {
function normalize(str) {
return str.toLowerCase().replace(/\s+/g, "").split("").sort().join("");
}

return normalize(str1) == normalize(str2);
}

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

function calculateTotalSpentByCategory(transactions) {
return [];
let result = [];

for (let i = 0; i < transactions.length; i++) {
var transaction = transactions[i];
var categoryExists = false;

for (let j = 0; j < result.length; j++) {
if (result[j].category == transaction.category) {
result[j].totalSpent += transaction.price;
categoryExists = true;
break;
}
}

if (!categoryExists) {
result.push({
category: transaction.category,
totalSpent: transaction.price,
});
}
}

return result;
}

module.exports = calculateTotalSpentByCategory;
11 changes: 9 additions & 2 deletions week-2/week-2-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
*/

function findLargestElement(numbers) {

let max = numbers[0];

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

module.exports = findLargestElement;
module.exports = findLargestElement;
59 changes: 58 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,63 @@
Once you've implemented the logic, test your code by running
*/

class Calculator {}
class Calculator {
constructor() {
this.result = 0;
}

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

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

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

divide(num) {
if (num == 0) {
throw new Error("Cannot divide by zero");
}
this.result /= num;
return this.result;
}

clear() {
this.result = 0;
return this.result;
}

getResult() {
return this.result;
}

calculate(inputExpression) {
const expression = inputExpression.replace(/\s+/g, "");
const isValidExpression = /^[0-9+\-*/().]+$/.test(expression);

if (!isValidExpression) {
throw new Error("Invalid Expression");
}

try {
this.result = eval(expression);
} catch (error) {
throw new Error("Invalid Expression");
}

if (this.result == Infinity) {
throw new Error("Cannot divide a number by 0");
}

return this.result;
}
}

module.exports = Calculator;
33 changes: 33 additions & 0 deletions week-2/week-2-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,40 @@
*/

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(indexOfTodo, updatedTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todos.length) {
this.todos[indexOfTodo] = 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;
16 changes: 14 additions & 2 deletions week-2/week-2-js/medium/countVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
*/

function countVowels(str) {
// Your code here
// Your code here
const vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];
let count = 0;

for (let i = 0; i < str.length; i++) {
for (let j = 0; j < vowels.length; j++) {
if (str[i] == vowels[j]) {
count++;
}
}
}

return count;
}

module.exports = countVowels;
module.exports = countVowels;
6 changes: 6 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,12 @@
*/

function isPalindrome(str) {
let str1 = str.replace(/[\W_]/g, "").toLowerCase();
for (let i = 0; i < str1.length / 2; i++) {
if (str1[i] != str1[str1.length - i - 1]) {
return false;
}
}
return true;
}

Expand Down
16 changes: 14 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,17 @@ There is no automated test for this one, this is more for you to understand time
*/

function calculateTime(n) {
return 0.01;
}
const startTime = new Date().getTime();

let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
const endTime = new Date().getTime();

return (endTime - startTime) / 1000;
}

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