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

function isAnagram(str1, str2) {

let first = str1.toLowerCase().split("").sort().join("");
let second = str2.toLowerCase().split("").sort().join("");
if (first == second) return true;
else 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 spent = {};
for (let i = 0; i < transactions.length; i++) {
if (spent[transactions[i].category]) {
spent[transactions[i].category] += transactions[i].price;
} else {
spent[transactions[i].category] = transactions[i].price;
}
}

var keys = Object.keys(spent);
var ans = [];
for (var i = 0; i < keys.length; i++) {
var obj = { category: keys[i], totalSpent: spent[keys[i]] };
ans.push(obj);
}
return ans;
}

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

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

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

remove(indexOfTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todoList.length) {
this.todoList.splice(indexOfTodo, 1);
}
}

update(index, updatedTodo) {
if (index >= 0 && index < this.todoList.length) {
this.todoList[index] = updatedTodo;
}
}

getAll() {
return this.todoList;
}

get(indexOfTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todoList.length) {
return this.todoList[indexOfTodo];
}
return null;
}

clear() {
this.todoList.length = 0;
}
}

module.exports = Todo;
21 changes: 20 additions & 1 deletion 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@
Once you've implemented the logic, test your code by running
- `npm run test-palindrome`
*/
function transform(str) {
let ans = "";
for (let i = 0; i < str.length; i++) {
if (
str[i] == "." ||
str[i] == " " ||
str[i] == "?" ||
str[i] == "!" ||
str[i] == ","
) {
} else {
ans += str[i];
}
}
return ans.toLowerCase();
}

function isPalindrome(str) {
return true;
let newStr = transform(str);
let revstr = newStr.split("").reverse().join("");
if (revstr == newStr) return true;
else return false;
}

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

function calculateTime(n) {
return 0.01;
}
let t1 = new Date();
let first = t1.getTime();
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
let t2 = new Date();
let second = t2.getTime();
console.log(second - first + " miliseconds");
}

calculateTime(10000000);
5 changes: 5 additions & 0 deletions 02-async-js/easy/1-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let count = 0;
setInterval(() => {
console.clear();
console.log(count++);
}, 1000);
2 changes: 1 addition & 1 deletion 02-async-js/easy/1-counter.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 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
13 changes: 13 additions & 0 deletions 02-async-js/easy/2-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let count = 0;
function timer() {
setTimeout(() => {
console.clear();
console.log(count++);
timer();
}, 1000);
}
setTimeout(() => {
console.clear();
console.log(count++);
timer();
}, 1000);
13 changes: 13 additions & 0 deletions 02-async-js/easy/3-read-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const fs = require("node:fs");

fs.readFile("read.txt", "utf-8", (err, data) => {
if (err) throw err;
else {
console.log(data);
}
});

for (let i = 0; i < 10000000000; i++) {
let count;
count++;
}
11 changes: 11 additions & 0 deletions 02-async-js/easy/4-write-to-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require("node:fs");

fs.writeFile(
"write.txt",
"I am writing to this file using fs lib.",
"utf-8",
(err, data) => {
if (err) throw err;
else console.log("File updated");
}
);
1 change: 1 addition & 0 deletions 02-async-js/easy/abc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Helo Nemo
1 change: 1 addition & 0 deletions 02-async-js/easy/read.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
My content is stolen.
1 change: 1 addition & 0 deletions 02-async-js/easy/write.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am writing to this file using fs lib.
25 changes: 25 additions & 0 deletions 02-async-js/medium/1-file-cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require("fs");

function clean(data) {
var dataArr = data.split(" ");

var ans = [];
for (let i = 0; i < dataArr.length; i++) {
if (dataArr[i].length == 0) {
} else {
ans.push(dataArr[i]);
}
}
ans = ans.join(" ");
return ans;
}

fs.readFile("clean.txt", "utf-8", (err, data) => {
if (err) throw err;
var cleanedData = clean(data);

fs.writeFile("clean.txt", cleanedData, "utf-8", (err, data) => {
if (err) throw err;
else console.log("file cleaned");
});
});
1 change: 1 addition & 0 deletions 02-async-js/medium/clean.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Helo i need some cleaning .
7 changes: 7 additions & 0 deletions 02-async-js/medium/clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
setInterval(() => {
console.clear();
let time = new Date();
var answer =
time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
console.log(answer);
}, 1000);
Empty file added 02-async-js/medium/read.txt
Empty file.