diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index fff61427..83c24c98 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -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; diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 20fbb943..ab002da8 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -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; diff --git a/01-js/hard/todo-list.js b/01-js/hard/todo-list.js index 7c9c1806..5f2aa45d 100644 --- a/01-js/hard/todo-list.js +++ b/01-js/hard/todo-list.js @@ -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; diff --git a/01-js/medium/palindrome.js b/01-js/medium/palindrome.js index d8fe2d8f..5ccb1162 100644 --- a/01-js/medium/palindrome.js +++ b/01-js/medium/palindrome.js @@ -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; diff --git a/01-js/medium/times.js b/01-js/medium/times.js index eb125cc2..8c2876a8 100644 --- a/01-js/medium/times.js +++ b/01-js/medium/times.js @@ -8,5 +8,15 @@ Hint - use Date class exposed in JS */ function calculateTime(n) { - return 0.01; -} \ No newline at end of file + 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); diff --git a/02-async-js/easy/1-counter.js b/02-async-js/easy/1-counter.js new file mode 100644 index 00000000..abc39c60 --- /dev/null +++ b/02-async-js/easy/1-counter.js @@ -0,0 +1,5 @@ +let count = 0; +setInterval(() => { + console.clear(); + console.log(count++); +}, 1000); diff --git a/02-async-js/easy/1-counter.md b/02-async-js/easy/1-counter.md index 54483eab..e241b799 100644 --- a/02-async-js/easy/1-counter.md +++ b/02-async-js/easy/1-counter.md @@ -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 \ No newline at end of file +It should go up as time goes by in intervals of 1 second diff --git a/02-async-js/easy/2-counter.js b/02-async-js/easy/2-counter.js new file mode 100644 index 00000000..302670d5 --- /dev/null +++ b/02-async-js/easy/2-counter.js @@ -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); diff --git a/02-async-js/easy/3-read-from-file.js b/02-async-js/easy/3-read-from-file.js new file mode 100644 index 00000000..564c1856 --- /dev/null +++ b/02-async-js/easy/3-read-from-file.js @@ -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++; +} diff --git a/02-async-js/easy/4-write-to-file.js b/02-async-js/easy/4-write-to-file.js new file mode 100644 index 00000000..ad53714a --- /dev/null +++ b/02-async-js/easy/4-write-to-file.js @@ -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"); + } +); diff --git a/02-async-js/easy/abc.txt b/02-async-js/easy/abc.txt new file mode 100644 index 00000000..81e8e261 --- /dev/null +++ b/02-async-js/easy/abc.txt @@ -0,0 +1 @@ +Helo Nemo \ No newline at end of file diff --git a/02-async-js/easy/read.txt b/02-async-js/easy/read.txt new file mode 100644 index 00000000..efb1f859 --- /dev/null +++ b/02-async-js/easy/read.txt @@ -0,0 +1 @@ +My content is stolen. \ No newline at end of file diff --git a/02-async-js/easy/write.txt b/02-async-js/easy/write.txt new file mode 100644 index 00000000..32c0ee3e --- /dev/null +++ b/02-async-js/easy/write.txt @@ -0,0 +1 @@ +I am writing to this file using fs lib. \ No newline at end of file diff --git a/02-async-js/medium/1-file-cleaner.js b/02-async-js/medium/1-file-cleaner.js new file mode 100644 index 00000000..d5c4b10e --- /dev/null +++ b/02-async-js/medium/1-file-cleaner.js @@ -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"); + }); +}); diff --git a/02-async-js/medium/clean.txt b/02-async-js/medium/clean.txt new file mode 100644 index 00000000..39bb0be9 --- /dev/null +++ b/02-async-js/medium/clean.txt @@ -0,0 +1 @@ +Helo i need some cleaning . \ No newline at end of file diff --git a/02-async-js/medium/clock.js b/02-async-js/medium/clock.js new file mode 100644 index 00000000..92694508 --- /dev/null +++ b/02-async-js/medium/clock.js @@ -0,0 +1,7 @@ +setInterval(() => { + console.clear(); + let time = new Date(); + var answer = + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(); + console.log(answer); +}, 1000); diff --git a/02-async-js/medium/read.txt b/02-async-js/medium/read.txt new file mode 100644 index 00000000..e69de29b