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
8 changes: 7 additions & 1 deletion 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
*/

function isAnagram(str1, str2) {

str1 = str1.toUpperCase().split('').sort().join('');
str2 = str2.toUpperCase().split('').sort().join('');
if(str1 == str2)
return true;
return false;
}

module.exports = isAnagram;

// isAnagram('Debit Card', 'Bad Credit');
19 changes: 18 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
var spentEstimate = {}
let ans = [];
for(var i=0;i<transactions.length;i++){
var t = transactions[i];
if(spentEstimate[t.category]){
spentEstimate[t.category]+=t.price;
} else {
spentEstimate [t.category] = t.price
}
}
var keys = Object.keys(spentEstimate)
for(var i =0;i<keys.length;i++){
ans.push({
category: keys[i],
totalSpent: spentEstimate[keys[i]]
})
}
return ans;
}

module.exports = calculateTotalSpentByCategory;
49 changes: 48 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,53 @@
- `npm run test-calculator`
*/

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

clear(){
this.result = 0;
}

getResult(){
return this.result
}

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

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

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

divide(num){
if(num == 0) {
throw Error('Number cannot be divided by 0');
}
else
this.result /=num;
}

calculate(expression) {
const sanitizedExpression = expression.replace(/\s+/g, ""); // Remove all whitespace characters from the expression
const parsedExpression = eval(sanitizedExpression); // Evaluate the sanitized expression using the eval function
if (isNaN(parsedExpression)) {
throw new Error("Invalid expression");
}

if (!isFinite(parsedExpression)) {
throw new Error("Division by zero");
}

this.result = parsedExpression;
}

}

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 @@ -12,7 +12,38 @@
*/

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

add(todo) {
this.todos.push(todo);
}

remove(indexOfTodo) {
this.todos.splice(indexOfTodo, 1);
}

update(index, updatedTodo) {
if(index > this.todos.length-1)
this.getAll();
else
this.todos[index] = updatedTodo;
}

getAll() {
return this.todos;
}

get(indexOfTodo) {
if(indexOfTodo > this.todos.length-1)
return null
return this.todos[indexOfTodo];
}

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

module.exports = Todo;
20 changes: 20 additions & 0 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@
*/

function isPalindrome(str) {
var i = 0, j = str.length - 1;
while (i <= j) {
if (!isAlphabetic(str[i])) {
i++;
continue;
}
if (!isAlphabetic(str[j])) {
j--;
continue;
}
if (str[i].toLowerCase() != str[j].toLowerCase()) {
return false;
}
i++;
j--;
}
return true;
}

function isAlphabetic(char) {
return /[a-zA-Z]/.test(char);
}

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

function calculateTime(n) {
return 0.01;
}
let startTime = new Date().getTime();
let sum = 0;
for(var i=1;i<=n;i++){
// console.log(i)
sum+=i;
}
let endTime= new Date().getTime();
return (endTime-startTime)/1000;
}

console.log(calculateTime(1000000000));
5 changes: 5 additions & 0 deletions 02-async-js/easy/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var counter = 1;
setInterval(()=>{
console.clear()
console.log(counter++)
},1000)
9 changes: 9 additions & 0 deletions 02-async-js/easy/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var counter = 1;

function callback(){
console.clear()
console.log(counter++)
setTimeout(callback,1000)
}

setTimeout(callback,1000)
10 changes: 10 additions & 0 deletions 02-async-js/easy/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require("fs")

fs.readFile('a.txt','utf8',function (err, data){
if(err){
console.error(err);
return;
} else {
console.log(data);
}
})
11 changes: 11 additions & 0 deletions 02-async-js/easy/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs')

let data = 'Namaste';

fs.writeFile('a.txt',data,'utf8',function (err){
if(err){
console.error(err);
return;
}
console.log('Check the file mostly content had been overwritten')
})
1 change: 1 addition & 0 deletions 02-async-js/easy/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Namaste
12 changes: 11 additions & 1 deletion 02-async-js/hard (promises)/1-promisify-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
*/

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

wait(5)
.then(() => {
console.log('5 seconds have passed');
});
8 changes: 6 additions & 2 deletions 02-async-js/hard (promises)/2-sleep-completely.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* During this time the thread should not be able to do anything else.
*/

function sleep (seconds) {

function sleep(milliseconds) {
const startTime = Date.now();
let currentTime = startTime;
while (currentTime - startTime < milliseconds) {
currentTime = Date.now();
}
}
34 changes: 28 additions & 6 deletions 02-async-js/hard (promises)/3-promise-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@


function waitOneSecond() {

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

function waitTwoSecond() {

function waitTwoSeconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 2000);
});
}

function waitThreeSecond() {

function waitThreeSeconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 3000);
});
}

function calculateTime() {
const startTime = Date.now();

// Wait for all three promises to resolve using Promise.all
Promise.all([waitOneSecond(), waitTwoSeconds(), waitThreeSeconds()]);

const endTime = Date.now();
const totalTime = endTime - startTime;

console.log(`All promises resolved in ${totalTime} milliseconds`);
}

}
calculateTime();
34 changes: 28 additions & 6 deletions 02-async-js/hard (promises)/4-promise-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@
*/

function waitOneSecond() {

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

function waitTwoSecond() {

function waitTwoSeconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 2000);
});
}

function waitThreeSecond() {

function waitThreeSeconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, 3000);
});
}

function calculateTime() {
const startTime = Date.now();

}
// Sequentially call all three functions
waitOneSecond();
waitTwoSeconds();
waitThreeSeconds();

const endTime = Date.now();
const totalTime = endTime - startTime;

console.log(`All promises resolved sequentially in ${totalTime} milliseconds`);
}
35 changes: 35 additions & 0 deletions 02-async-js/medium/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require('fs')

function clean(data){
var arr = data.split(" ");
var answerArray = [];
for(var i = 0;i<arr.length;i++){
if(arr[i].length===0){
continue;
}
else {
answerArray.push(arr[i]);
}
}
var asnwerString = answerArray.join(" ");
// console.log(asnwerString)
return asnwerString;
}

function fileRead(err, data){
if(err){
console.error(err)
return;
}

let cleanedData = clean(data);

fs.writeFile('a.txt',cleanedData,'utf8',function(err){
if(err){
console.error(err)
}
console.log('Done')
})
}

fs.readFile('a.txt','utf8',fileRead)
Loading