From 9dca9bd51f0b0a3e320b9b1d9b6ac2a6a914bb02 Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 11:40:00 +0530 Subject: [PATCH 01/11] Anagram function written and tests passed. --- 01-js/easy/anagram.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/01-js/easy/anagram.js b/01-js/easy/anagram.js index fff61427..8d50d981 100644 --- a/01-js/easy/anagram.js +++ b/01-js/easy/anagram.js @@ -7,8 +7,27 @@ - `npm run test-anagram` */ -function isAnagram(str1, str2) { -} + function sort(str) { + var array = str.split(""); // ["d", "c", "a", "b"] + // array = array.map(char => char.toLowerCase()); + //OR + for (var i = 0; i < array.length; i++) { + array[i] = array[i].toLowerCase(); + console.log(array[i]); + } + array = array.sort(); + var sortedString = array.join("") + return sortedString; + } + + +function isAnagram(str1, str2) { + if (sort(str1) == sort(str2)) { + return true; + } else { + return false; + } +} module.exports = isAnagram; From 90b9329e0b6cbdbd6c9d56ab6de9c8b916278790 Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 11:41:39 +0530 Subject: [PATCH 02/11] expenditure-analysis function written and tests passed --- 01-js/easy/expenditure-analysis.js | 54 +++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/01-js/easy/expenditure-analysis.js b/01-js/easy/expenditure-analysis.js index 20fbb943..0c00f82a 100644 --- a/01-js/easy/expenditure-analysis.js +++ b/01-js/easy/expenditure-analysis.js @@ -8,8 +8,60 @@ - `npm run test-expenditure-analysis` */ + +// function calculateTotalSpentByCategory(transactions) { +// var spent = {}; +// for(var i = 0 ; i < transactions.length; i++ ){ +// var t = transactions[i]; +// if(spent[t.category]){ +// spent[t.category] += t.price; +// } +// else { +// spent[t.category] = t.price ; +// } +// } + +// var keys = Object.keys(spent); + +// var answer = []; +// for( var i = 0 ; i < keys.length; i++){ +// var category = keys[i]; +// var obj = { +// category : category, +// totalSpent : spent[category] +// }; +// answer.push(obj); + +// } +// return answer; + +// } + function calculateTotalSpentByCategory(transactions) { - return []; + var spent = {}; + var categories = []; + for(var i = 0 ; i < transactions.length; i++ ){ + var t = transactions[i]; + if(spent[t.category]){ + spent[t.category] += t.price; + } else { + spent[t.category] = t.price; + categories.push(t.category); + } + } + + var answer = []; + for( var i = 0 ; i < categories.length; i++){ + var category = categories[i]; + var obj = { + category : category, + totalSpent : spent[category] + }; + answer.push(obj); + } + return answer; } module.exports = calculateTotalSpentByCategory; + + From 51cee00f771af492d4dc33de2ae6b3df3a63fc2d Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 11:43:57 +0530 Subject: [PATCH 03/11] Palindrome function written and tests passed --- 01-js/medium/palindrome.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/01-js/medium/palindrome.js b/01-js/medium/palindrome.js index d8fe2d8f..fcf28eb0 100644 --- a/01-js/medium/palindrome.js +++ b/01-js/medium/palindrome.js @@ -5,9 +5,37 @@ Once you've implemented the logic, test your code by running - `npm run test-palindrome` */ +function check(str) { + var answer = ""; + for(var i = str.length - 1 ; i >= 0 ; i--){ + answer += str[i]; + } + console.log(answer); + return answer; +} + +function transform(str){ + var answer = ""; + for(var i = 0 ; i < str.length ; i++){ + if(str[i] == " " || str[i] == "?" || str[i] == "," || str[i] == "!" || str[i] == "."){ + +}else { + answer += str[i]; +} + } + console.log(answer); + return answer; +} function isPalindrome(str) { - return true; + str = str.toLowerCase(); + console.log(str); + str = transform(str); + console.log(str); + if(str === check(str)){ + return true; + } + return false; } module.exports = isPalindrome; From eb3d92b8b04310819b161bfb1c0e250fa2986ef7 Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 11:53:00 +0530 Subject: [PATCH 04/11] function that calculates time --- 01-js/medium/times.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/01-js/medium/times.js b/01-js/medium/times.js index eb125cc2..aa7e1ae4 100644 --- a/01-js/medium/times.js +++ b/01-js/medium/times.js @@ -8,5 +8,12 @@ Hint - use Date class exposed in JS */ function calculateTime(n) { - return 0.01; -} \ No newline at end of file + var start = new Date().getTime(); + var sum = 0 ; + for(var i = 1 ; i < n ; i++){ + sum += i; + } + var end = new Date().getTime(); + console.log((end - start)/1000); +} +calculateTime(1000000000); \ No newline at end of file From c7993301f4026607e8fb163f3451dd1f097f466c Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:25:09 +0530 Subject: [PATCH 05/11] counter using setTimeout --- 02-async-js/easy/counter_2.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 02-async-js/easy/counter_2.js diff --git a/02-async-js/easy/counter_2.js b/02-async-js/easy/counter_2.js new file mode 100644 index 00000000..97b5be37 --- /dev/null +++ b/02-async-js/easy/counter_2.js @@ -0,0 +1,10 @@ +var counter = 1; + +function stopwatch() { + console.clear(); + console.log(counter); + counter = counter + 1; + setTimeout(stopwatch, 1000); +} + +stopwatch(); \ No newline at end of file From da3c33ff0de1b41950148f90fd0f51f311a0dee8 Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:27:13 +0530 Subject: [PATCH 06/11] counter implemented --- 02-async-js/easy/counter.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 02-async-js/easy/counter.js diff --git a/02-async-js/easy/counter.js b/02-async-js/easy/counter.js new file mode 100644 index 00000000..8724c614 --- /dev/null +++ b/02-async-js/easy/counter.js @@ -0,0 +1,8 @@ +var counter = 1; + +function stopwatch(){ + console.clear(); + console.log(counter); + counter= counter + 1; +} +setInterval(stopwatch, 1000); From 8527b3b8a5c658845b6a3c49c981cec5e1f78581 Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:28:24 +0530 Subject: [PATCH 07/11] reading from a file --- 02-async-js/easy/read-from-file.js | 11 +++++++++++ 02-async-js/easy/read.txt | 1 + 2 files changed, 12 insertions(+) create mode 100644 02-async-js/easy/read-from-file.js create mode 100644 02-async-js/easy/read.txt diff --git a/02-async-js/easy/read-from-file.js b/02-async-js/easy/read-from-file.js new file mode 100644 index 00000000..b3aa2664 --- /dev/null +++ b/02-async-js/easy/read-from-file.js @@ -0,0 +1,11 @@ +const fs = require('fs'); +const path = require('path'); +function read(err, data) { + if (err) { + console.error(err); + return; + } + console.log(data); +} +const filePath = path.join(__dirname, 'read.txt'); +fs.readFile(filePath, 'utf8', read); diff --git a/02-async-js/easy/read.txt b/02-async-js/easy/read.txt new file mode 100644 index 00000000..0066bb8c --- /dev/null +++ b/02-async-js/easy/read.txt @@ -0,0 +1 @@ +I AM A MAN WHO DRINKS TEA! From e1c42abc65d1f542135b465e5789cdec7a951b7b Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:28:50 +0530 Subject: [PATCH 08/11] writing to a file --- 02-async-js/easy/write-to-file.js | 9 +++++++++ 02-async-js/easy/write.txt | 1 + 2 files changed, 10 insertions(+) create mode 100644 02-async-js/easy/write-to-file.js create mode 100644 02-async-js/easy/write.txt diff --git a/02-async-js/easy/write-to-file.js b/02-async-js/easy/write-to-file.js new file mode 100644 index 00000000..39750406 --- /dev/null +++ b/02-async-js/easy/write-to-file.js @@ -0,0 +1,9 @@ +const fs = require('fs'); +const path = require('path'); + +const data = "I AM A MAN WHO DRINKS TEA."; +const filePath = path.join(__dirname, 'write.txt'); +fs.writeFile(filePath, data, "utf8" , function (err) { + if (err) throw err; + console.log("Wrote to the file successfully."); +}); \ 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..03d981b0 --- /dev/null +++ b/02-async-js/easy/write.txt @@ -0,0 +1 @@ +I AM A MAN WHO DRINKS TEA. \ No newline at end of file From 5699307d1cbb26aed8841568993651ddbb8ffa3e Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:29:22 +0530 Subject: [PATCH 09/11] file cleaner --- 02-async-js/medium/clean.txt | 3 +++ 02-async-js/medium/cleaned.txt | 1 + 02-async-js/medium/file-cleaner.js | 34 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 02-async-js/medium/clean.txt create mode 100644 02-async-js/medium/cleaned.txt create mode 100644 02-async-js/medium/file-cleaner.js diff --git a/02-async-js/medium/clean.txt b/02-async-js/medium/clean.txt new file mode 100644 index 00000000..94bf3997 --- /dev/null +++ b/02-async-js/medium/clean.txt @@ -0,0 +1,3 @@ +I AM A LOW + KEY SOFTWARE DEVELOPER + AND A HACKER. \ No newline at end of file diff --git a/02-async-js/medium/cleaned.txt b/02-async-js/medium/cleaned.txt new file mode 100644 index 00000000..4d257e34 --- /dev/null +++ b/02-async-js/medium/cleaned.txt @@ -0,0 +1 @@ +I AM A LOW KEY SOFTWARE DEVELOPER AND A HACKER. \ No newline at end of file diff --git a/02-async-js/medium/file-cleaner.js b/02-async-js/medium/file-cleaner.js new file mode 100644 index 00000000..be96cfc9 --- /dev/null +++ b/02-async-js/medium/file-cleaner.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const path = require('path'); + +function clean(data) { + data = data.replace(/\s+/g, ' ').trim(); // replace(/\s+/g, ' ') is a regular expression that matches one or more spaces and replaces them with a single space. The trim() method removes whitespace from both ends of a string. + var array = data.split(" "); + var answer = []; + for (var i = 0; i < array.length; i++) { + if (array[i] !== "") { + answer.push(array[i]); + } + } + var result = answer.join(" "); + return result; +} + +function fileRead(err, data) { + if (err) { + console.error(err); + return; + } + + let cleanedData = clean(data); + console.log(cleanedData); + const filePath = path.join(__dirname, 'cleaned.txt'); + // to ensure that the data is fully processed before it's written to the file fs.writeFileSync function, which is the synchronous version of fs.writeFile is used. + fs.writeFileSync(filePath, cleanedData, "utf8", function (err) { + if (err) throw err; + console.log("Wrote to the file successfully."); + }); +} + +const filePath = path.join(__dirname, 'clean.txt'); +fs.readFile(filePath, 'utf8', fileRead); \ No newline at end of file From c5da174331bf0cfcfc4969feab5e8a7214931fbd Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:30:08 +0530 Subject: [PATCH 10/11] clock implemented --- 02-async-js/medium/clock.js | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 02-async-js/medium/clock.js diff --git a/02-async-js/medium/clock.js b/02-async-js/medium/clock.js new file mode 100644 index 00000000..e3874dd4 --- /dev/null +++ b/02-async-js/medium/clock.js @@ -0,0 +1,40 @@ + +function printTime() { + var d = new Date(); + var hours = d.getHours(); + var minutes = d.getMinutes(); + var seconds = d.getSeconds(); + var ampm; + if (hours >= 12) { + ampm = 'PM'; + } else { + ampm = 'AM'; + } + + // Convert hours from 24-hour to 12-hour format and prepend 0 if needed + hours = hours % 12; + if (hours === 0) { + hours = 12; + } + // Prepend 0 to minutes and seconds if needed + if (hours < 10) { + hours = '0' + hours; + } + + if (minutes < 10) { + minutes = '0' + minutes; + } + + if (seconds < 10) { + seconds = '0' + seconds; + } + var time = hours + ":" + minutes + ":" + seconds + " " + ampm; + console.log(time); + } + + function clock(){ + console.clear(); + printTime(); + } + + setInterval(clock, 1000); \ No newline at end of file From b56ba1afd43531ff60e17f4d44b3391ba18db98d Mon Sep 17 00:00:00 2001 From: Vasant18 Date: Mon, 8 Jan 2024 13:30:35 +0530 Subject: [PATCH 11/11] Gitignore file --- .gitignore | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d562c8c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,138 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + + +#.vscode files +../.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json