Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week1-Assignment #364

Open
wants to merge 11 commits into
base: hkirat/with-tests
Choose a base branch
from
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
138 changes: 138 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
23 changes: 21 additions & 2 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
54 changes: 53 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;


30 changes: 29 additions & 1 deletion 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
11 changes: 9 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ Hint - use Date class exposed in JS
*/

function calculateTime(n) {
return 0.01;
}
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);
8 changes: 8 additions & 0 deletions 02-async-js/easy/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var counter = 1;

function stopwatch(){
console.clear();
console.log(counter);
counter= counter + 1;
}
setInterval(stopwatch, 1000);
10 changes: 10 additions & 0 deletions 02-async-js/easy/counter_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var counter = 1;

function stopwatch() {
console.clear();
console.log(counter);
counter = counter + 1;
setTimeout(stopwatch, 1000);
}

stopwatch();
11 changes: 11 additions & 0 deletions 02-async-js/easy/read-from-file.js
Original file line number Diff line number Diff line change
@@ -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);
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 @@
I AM A MAN WHO DRINKS TEA!
9 changes: 9 additions & 0 deletions 02-async-js/easy/write-to-file.js
Original file line number Diff line number Diff line change
@@ -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.");
});
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 A MAN WHO DRINKS TEA.
3 changes: 3 additions & 0 deletions 02-async-js/medium/clean.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I AM A LOW
KEY SOFTWARE DEVELOPER
AND A HACKER.
1 change: 1 addition & 0 deletions 02-async-js/medium/cleaned.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I AM A LOW KEY SOFTWARE DEVELOPER AND A HACKER.
40 changes: 40 additions & 0 deletions 02-async-js/medium/clock.js
Original file line number Diff line number Diff line change
@@ -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);
Loading