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

Assignments week 1 #345

Open
wants to merge 8 commits into
base: main
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
7 changes: 6 additions & 1 deletion 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
43 changes: 42 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 7 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
- `npm run test-calculator`
*/

class Calculator {}
class Calculator {
constructor(result) {
this.result = result;
}


}

module.exports = Calculator;
5 changes: 4 additions & 1 deletion 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
19 changes: 17 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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));
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 @@
let counter = 0;

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

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

run();
6 changes: 6 additions & 0 deletions 02-async-js/easy/readfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const fs = require("fs");

fs.readFile("./text.txt", "utf8", (err, data) => {
if (err) throw err;
console.log(data);
});
6 changes: 6 additions & 0 deletions 02-async-js/easy/writefile.js
Original file line number Diff line number Diff line change
@@ -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");
});
23 changes: 23 additions & 0 deletions 02-async-js/medium/clock.js
Original file line number Diff line number Diff line change
@@ -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);
31 changes: 31 additions & 0 deletions 02-async-js/medium/file-cleaner.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
1 change: 1 addition & 0 deletions 02-async-js/medium/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
asda asd d ads as asd as dadasd