diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index fff61427..bf36441f 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -8,7 +8,12 @@ */ function isAnagram(str1, str2) { - + console.log( + str1.split("").sort().join("") === str2.split("").sort().join("") + ? true + : false + ); } +isAnagram("bat", "tb"); module.exports = isAnagram; diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 20fbb943..feb9bde2 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -7,9 +7,50 @@ Once you've implemented the logic, test your code by running - `npm run test-expenditure-analysis` */ +const items = [ + { + itemName: "Mobile Phone", + category: "Electronics", + price: 100, + timestamp: Date.now(), + }, + { + itemName: "Shirt1", + category: "Cloths", + price: 500, + timestamp: Date.now(), + }, + { + itemName: "Shirt2", + category: "Cloths", + price: 1100, + timestamp: Date.now(), + }, + { + itemName: "Bed", + category: "Furniture", + price: 200, + timestamp: Date.now(), + }, +]; function calculateTotalSpentByCategory(transactions) { - return []; + const cateoryTotalAmount = {}; + + transactions.forEach((transaction) => { + const { category, price } = transaction; + if (cateoryTotalAmount[category]) { + cateoryTotalAmount[category] += price; + } else { + cateoryTotalAmount[category] = price; + } + }); + + return Object.keys(cateoryTotalAmount).map((category) => ({ + [category]: cateoryTotalAmount[category], + })); } +console.log(calculateTotalSpentByCategory(items)); + module.exports = calculateTotalSpentByCategory; diff --git a/01-js/hard/calculator.js b/01-js/hard/calculator.js index 82d48229..60ad6e85 100644 --- a/01-js/hard/calculator.js +++ b/01-js/hard/calculator.js @@ -17,6 +17,12 @@ - `npm run test-calculator` */ -class Calculator {} +class Calculator { + constructor(result) { + this.result = result; + } + + +} module.exports = Calculator; diff --git a/01-js/medium/palindrome.js b/01-js/medium/palindrome.js index d8fe2d8f..c5d864a7 100644 --- a/01-js/medium/palindrome.js +++ b/01-js/medium/palindrome.js @@ -7,7 +7,10 @@ */ function isPalindrome(str) { - return true; + str = str.toLowerCase(); + return str === str.split("").reverse().join(""); } +console.log(isPalindrome("Hannah")); + module.exports = isPalindrome; diff --git a/01-js/medium/times.js b/01-js/medium/times.js index eb125cc2..39a72207 100644 --- a/01-js/medium/times.js +++ b/01-js/medium/times.js @@ -7,6 +7,21 @@ Try running it for Hint - use Date class exposed in JS */ +const s = new Date(); +let sec = s.getSeconds(); + function calculateTime(n) { - return 0.01; -} \ No newline at end of file + let start = new Date().getTime(); + + let sum = 0; + for (let i = 1; i <= n; i++) { + sum += i; + } + console.log(sum); + + let end = new Date().getTime(); + let time = (end - start) / 1000; + return time; +} + +console.log(calculateTime(10000)); diff --git a/02-async-js/easy/counter.js b/02-async-js/easy/counter.js new file mode 100644 index 00000000..3ad5ca76 --- /dev/null +++ b/02-async-js/easy/counter.js @@ -0,0 +1,8 @@ +let counter = 0; + +function setCounter() { + console.clear(); + counter++; + console.log(counter); +} +setInterval(setCounter, 1000); diff --git a/02-async-js/easy/counter2.js b/02-async-js/easy/counter2.js new file mode 100644 index 00000000..aa05392f --- /dev/null +++ b/02-async-js/easy/counter2.js @@ -0,0 +1,10 @@ +let counter = 0; + +function run() { + console.clear(); + console.log(counter); + counter++; + setTimeout(run, 1 * 1000); +} + +run(); diff --git a/02-async-js/easy/readfile.js b/02-async-js/easy/readfile.js new file mode 100644 index 00000000..b5e05410 --- /dev/null +++ b/02-async-js/easy/readfile.js @@ -0,0 +1,6 @@ +const fs = require("fs"); + +fs.readFile("./text.txt", "utf8", (err, data) => { + if (err) throw err; + console.log(data); +}); diff --git a/02-async-js/easy/writefile.js b/02-async-js/easy/writefile.js new file mode 100644 index 00000000..3b35c25f --- /dev/null +++ b/02-async-js/easy/writefile.js @@ -0,0 +1,6 @@ +const fs = require("fs"); + +fs.writeFile("./text.txt", "This is the change", "utf8", (err) => { + if (err) throw err; + console.log("done"); +}); diff --git a/02-async-js/medium/clock.js b/02-async-js/medium/clock.js new file mode 100644 index 00000000..d159f1c4 --- /dev/null +++ b/02-async-js/medium/clock.js @@ -0,0 +1,23 @@ +function printTime() { + var time = new Date(); + var format = time.getHours() > 12 ? "PM" : "AM"; + + var hours = time.getHours(); + hours = hours > 12 ? hours - 12 : hours; + + var mintue = time.getMinutes(); + mintue = mintue < 10 ? "0" + mintue : mintue; + + var sec = time.getSeconds(); + sec = sec < 10 ? "0" + sec : sec; + + const ans = hours + ":" + mintue + ":" + sec + " " + format; + console.log(ans); +} + +function clock() { + console.clear(); + printTime(); +} + +setInterval(clock, 1000); diff --git a/02-async-js/medium/file-cleaner.js b/02-async-js/medium/file-cleaner.js new file mode 100644 index 00000000..6a5bae54 --- /dev/null +++ b/02-async-js/medium/file-cleaner.js @@ -0,0 +1,31 @@ +// ## File cleaner +// Read a file, remove all the extra spaces and write it back to the same file. + +// For example, if the file input was +// ``` +// hello world my name is raman +// ``` + +// After the program runs, the output should be + +// ``` +// hello world my name is raman +// ``` + +const fs = require("fs"); + +function clean(data) { + data = data.replace(/\s+/g, " ").trim(); + return data; +} + +fs.readFile("./text.txt", "utf8", (err, data) => { + if (err) console.log(err); + data = clean(data); + // console.log(data); + + fs.writeFile("./text.txt", data, "utf8", (err) => { + if (err) console.log(err); + console.log(data); + }); +}); diff --git a/02-async-js/medium/text.txt b/02-async-js/medium/text.txt new file mode 100644 index 00000000..09f1387c --- /dev/null +++ b/02-async-js/medium/text.txt @@ -0,0 +1 @@ +asda asd d ads as asd as dadasd \ No newline at end of file