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 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; 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; + + 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; 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 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); 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 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! 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 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/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 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