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

easy and medium assignments done #336

Open
wants to merge 1 commit 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
14 changes: 12 additions & 2 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@
*/

function isAnagram(str1, str2) {

var str1LowerCase = str1.toLowerCase();
var str2LowerCase = str2.toLowerCase();
if(str1LowerCase.split("").sort().join("") == str2LowerCase.split("").sort().join("")){
return true;
}
else{
return false;
}
}

module.exports = isAnagram;
var ans = isAnagram("rasp","pasr");
console.log(ans);

module.exports = isAnagram;
30 changes: 28 additions & 2 deletions 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
var spendEstimates = {};

for(var i = 0; i < transactions.length; i++){
var t = transactions[i];

if(spendEstimates[t.category]){
spendEstimates[t.category] = spendEstimates[t.category] + t.price;
}
else{
spendEstimates[t.category] = t.price;
}
}

var keys = Object.keys(spendEstimates);

let answer = [];
for(var i = 0; i < keys.length; i++){
var category = keys[i];
var obj = {
category: category,
totalSpent: spendEstimates[category]
}
answer.push(obj);
}

console.log(answer);
return answer;
}

module.exports = calculateTotalSpentByCategory;
module.exports = calculateTotalSpentByCategory;
41 changes: 39 additions & 2 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,45 @@
- `npm run test-palindrome`
*/

function reverse(str){
var answer = "";
for(let i = str.length - 1;i >= 0; i--){
answer += str[i];
}
return answer;
}

function transform(str){
var answer = "";
for(let i = 0; i < str.length; i++){
if(str[i] == " " || str[i] == "?" || str[i] == "!" || str[i] == "," || str[i] == "."){

}
else{
answer += str[i];
}
}
return answer;
}

function isPalindrome(str) {
return true;
str = str.toLowerCase();
str = transform(str);
var reverseString = reverse(str);
reverseString = reverseString.toLowerCase();

if(str == reverseString){
return true;
}
else{
return false;
}
}

module.exports = isPalindrome;
var str1 = "race car";
var str2 = "jill";

console.log(isPalindrome(str1));
console.log(isPalindrome(str2));

module.exports = isPalindrome;
14 changes: 12 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ Hint - use Date class exposed in JS
*/

function calculateTime(n) {
return 0.01;
}
let startTime = new Date().getTime();
let sum = 0;
for(let i = 1; i < n; i++){
sum += i;
}
let endTime = new Date().getTime();
console.log((endTime - startTime) / 1000);
}

calculateTime(100);
calculateTime(100000);
calculateTime(1000000000);
9 changes: 9 additions & 0 deletions 02-async-js/easy/1-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var counter = 1;

function increaseCount(){
console.clear();
counter += 1;
console.log(counter);
}

setInterval(increaseCount, 1000);
10 changes: 10 additions & 0 deletions 02-async-js/easy/2-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var counter = 0;

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

setTimeout(increaseCount, 1000);
14 changes: 14 additions & 0 deletions 02-async-js/easy/3-read-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs');

fs.readFile('./sample-text.txt', 'utf-8', (err, data) => {
if(err){
console.error(err);
return;
}
console.log(data);
});

var counter = 0;
for(var i = 0; i < 10000000; i++){
counter += 1;
}
9 changes: 9 additions & 0 deletions 02-async-js/easy/4-write-to-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require('fs');

fs.writeFile('sample-text.txt','This is some sample data to write to the file.','utf8', (err) => {
if(err){
console.error(err);
return;
}
console.log('File has been written');
});
1 change: 1 addition & 0 deletions 02-async-js/easy/sample-text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is some sample data to write to the file.
27 changes: 27 additions & 0 deletions 02-async-js/medium/1-file-cleaner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require('fs');

fs.readFile('./temp-file.txt','utf8', (err,data) => {
if(err){
console.error(err);
return;
}
console.log(data);

function clean(data){
var arr = data.split(" ").filter(x => x!='');
var newData = arr.join(" ");
return newData;
};

const writeData = clean(data);

fs.writeFile('./temp-file.txt', writeData, 'utf8', (err) => {
if(err){
console.error(err);
return;
}
else{
console.log("File has been written successfully!");
}
});
});
45 changes: 45 additions & 0 deletions 02-async-js/medium/2-clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//HH:MM::SS (Eg. 13:45:23) format

function printTime(){
console.clear();

let currentDate = new Date();
const answer = `${currentDate.getHours()}:${currentDate.getMinutes()}:${currentDate.getSeconds()}`;
console.log(answer);
}

setInterval(printTime, 1000);

//HH:MM::SS AM/PM (Eg 01:45:23 PM) format

function printTime(){
console.clear();

let currentDate = new Date();

const sec = currentDate.getSeconds();
const min = currentDate.getMinutes();
const hour = () => {
currentDate.getHours();
if(currentDate.getHours() > 12){
return (currentDate.getHours() - 12);
}
else{
return currentDate.getHours();
}
}

const amOrPm = () => {
if(currentDate.getHours() > 12){
return "PM";
}
else{
return "AM";
}
}

console.log(`${hour()}:${min}:${sec} ${amOrPm()}`);
setTimeout(printTime,1000);
}

setTimeout(printTime,1000);
1 change: 1 addition & 0 deletions 02-async-js/medium/temp-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world my name is varshaa