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
5 changes: 5 additions & 0 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
*/

function isAnagram(str1, str2) {
str1 = str1.replace(/\s/g, "").toLowerCase();
str2 = str2.replace(/\s/g, "").toLowerCase();

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

return str1.split("").sort().join("") === str1.split("").sort().join("");
}

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

function calculateTotalSpentByCategory(transactions) {
return [];
const categoryTotals = transactions.reduce((acc, tx) => {
acc[tx.category] = (acc[tx.category] || 0) + tx.price;
return acc;
}, {});
return Object.entries(categoryTotals).map(([category, totalSpent]) => ({
category,
totalSpent,
}));
}

module.exports = calculateTotalSpentByCategory;
9 changes: 8 additions & 1 deletion 01-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 = 0; i < numbers.length; i++) {
if(numbers[i] > max) {
max = numbers[i];
}
}
return max;
}

module.exports = findLargestElement;
64 changes: 63 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,68 @@
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("Divide by zero is not allowed");
}

this.result /= num;
return this.result;
}

clear() {
this.result = 0;
}

getResult() {
return this.result;
}

calculate(expression) {
if(typeof expression !== "string") {
throw new Error("Expression must be string");
}

const sanitized = expression.replace(/\s/g, "");

if(/[^0-9+\-*().]/.test(sanitized)) {
throw new Error("Invalid characters in expression");
}

try {
const evalResult = new Function(`return ${sanitized}`)();

if(typeof evalResult !== "number" || isNaN(evalResult) || !isFinite(evalResult)) {
throw new Error("Invalid calculation");
}

this.result = evalResult;

return this.result;
} catch (err) {
throw new Error("Invalid expression");
}
}
}

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

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

add(todo) {
this.todos.push(todo);
}
remove(index) {
if(index < 0 || index > this.todos.length) {
throw new Error("Invalid Index");
}
this.todos.splice(index);
}
update(index, updatedTdodo) {
if(index < 0 || index > this.todos.length) {
throw new Error("Invalid Index");
}
this.todos[index] = updatedTdodo;
}
get(index) {
if(index < 0 || index > this.todos.length) {
throw new Error("Invalid Index");
}
return this.todos[index];
}

getAll() {
return [...this.todos];
}
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 vowels = "aeiouAEIOU";
let count = 0;

for(let char of str) {
if(vowels.includes(char)) {
count++;
}
}
return count;
}

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

function isPalindrome(str) {
return true;
const lowerStr = str.toLowerCase();
const reverseStr = lowerStr.split("").reverse().join("");
return lowerStr === reverseStr;
}

module.exports = isPalindrome;
13 changes: 12 additions & 1 deletion 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,16 @@ 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();

const timeInSec = (endTime - startTime) / 1000;

return timeInSec;
}
5 changes: 4 additions & 1 deletion week-2/01-async-js/easy/1-counter.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 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
It should go up as time goes by in intervals of 1 second


DONE - pahari
2 changes: 2 additions & 0 deletions week-2/01-async-js/easy/2-counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Without using setInterval, try to code a counter in Javascript. There is a hint



DONE - pahari;




Expand Down
24 changes: 24 additions & 0 deletions week-2/01-async-js/easy/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// function setCounter() {
// let count = 0;
// setInterval(() => {
// count++;
// console.log(count);
// }, 1000);
// }

// setCounter();



function setCounter() {
let count = 0;
function tick() {
count++;
console.log(count);
setTimeout(tick, 1000);
}

tick();
}

setCounter();
28 changes: 28 additions & 0 deletions week-2/01-async-js/easy/readfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require("fs");
const path = require("path");

console.log("start");

const filepath = path.join(__dirname, "sample.txt");

fs.readFile(filepath, "utf-8", (err, data) => {
if (err) {
console.error("Error reading file :", err);
return;
}
console.log("file data \n", data);
})

console.log("after start reading file");

function expence(iter) {
let sum = 0;
for(let i = 0; i<iter; i++) {
sum += i;
}
console.log("operation done :", sum);
}

expence(1e8);

console.log("end");
1 change: 1 addition & 0 deletions week-2/01-async-js/easy/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hii you are lazy
28 changes: 28 additions & 0 deletions week-2/01-async-js/easy/writefile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require("fs");
const path = require("path");

console.log("start");

const filepath = path.join(__dirname, "sample.txt");

fs.writeFile(filepath, "hii you are lazy", "utf-8", (err) => {
if(err) {
console.error("this is an error", err);
}

console.log("successfull write");
})

console.log("after start writing");

function expense(iter) {
let sum = 0;
for(let i = 0; i < iter; i++) {
sum += i;
}
console.log(sum);
}

expense(1e9);

console.log("end");
3 changes: 3 additions & 0 deletions week-2/01-async-js/hard (promises)/1-promisify-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/

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

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

function sleep(milliseconds) {
return new Promise((resolve) => {
const start = Date.now();
while (Date.now() - start < milliseconds) {

}
resolve();
})
}

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

function wait1(t) {

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

function wait2(t) {

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

function wait3(t) {

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

function calculateTime(t1, t2, t3) {
const start = Date.now();
return Promise.all([wait1(t1), wait2(t2), wait3(t3)]).then(() => {
const end = Date.now();

return end - start;
})
}

module.exports = calculateTime;
Loading