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

100xdevs Cohort 2 Week 1 to Week 4 assignments #1159

Open
wants to merge 11 commits into
base: master
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
20 changes: 20 additions & 0 deletions 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,27 @@
*/

function isAnagram(str1, str2) {
if (str1.length != str2.length) {
return false;
}
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
let charcount = 256;
let count = new Array(charcount);
for (let i = 0; i < charcount; i++){
count[i] = 0;
}
for (let i = 0; i < str1.length; i++) {
count[str1[i].charCodeAt(0)]++;
count[str2[i].charCodeAt(0)]--;
}

for (let i = 0; i < charcount; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}

module.exports = isAnagram;
17 changes: 16 additions & 1 deletion 01-js/easy/expenditure-analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@
*/

function calculateTotalSpentByCategory(transactions) {
return [];
let categoryMap = new Map();
transactions.forEach(item => {
if (categoryMap.has(item.category)){
categoryMap.set(item.category, categoryMap.get(item.category)+item.price);
} else {
categoryMap.set(item.category, item.price);
}
});

const result = [];
categoryMap.forEach((totalSpent,category) => {
result.push({"category" : category, "totalSpent" : totalSpent})
});
return result;
}



module.exports = calculateTotalSpentByCategory;
11 changes: 10 additions & 1 deletion 01-js/easy/findLargestElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
*/

function findLargestElement(numbers) {

let max = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;

}

module.exports = findLargestElement;
module.exports = findLargestElement;

50 changes: 49 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,54 @@
Once you've implemented the logic, test your code by running
*/

class Calculator {}
class Calculator {
constructor() {
this.result = 0;
}
add(number) {
this.result += number;
}
subtract(number) {
this.result -= number;
}
multiply(number) {
this.result *= number;
}
divide(number) {
if (number == 0){
throw new Error("Can't divide by zero");
}
this.result = this.result/number;
}
clear() {
this.result = 0;
}
getResult(){
return this.result;
}
calculate(expression) {
const temp = expression;
const cleanedExpression = temp.replace(/\s+/g, ''); // s+ matches one or more white spaces g is for matching global search replacing all occurence
const validExpression = /^[0-9+\-*/().]+$/.test(cleanedExpression); //regular expression to check if it contains only valid character for mathematical expression

if (!validExpression) {
throw new Error("Invalid expression"); //throw error for expression like 5 + abc
}

try {
this.result = eval(expression);
} catch (err) {
throw new Error("Invalid expression")
}
if (this.result === Infinity) {
//when trying to divide by zero eval result in Infinity to throw error for divide by 0 adding this check here
throw new Error("Cannot divide a number by 0")
}
return this.result;
}


}


module.exports = Calculator;
34 changes: 33 additions & 1 deletion 01-js/hard/todo-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,39 @@
*/

class Todo {

constructor() {
this.todos = [];
}
add(todo) {
this.todos.push(todo);
}
remove(indexOfTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todos.length){
this.todos.splice(indexOfTodo,1);
} else {
console.error("Invalid , Error in Remove, cnat remove");
}
}
update(index,updatedTodo){
if (index >= 0 && index < this.todos.length){
this.todos[index] = updatedTodo;
} else {
console.error("Invalid , Error in update, cnat update");
}
}
getAll(){
return this.todos;
}
get(indexOfTodo) {
if (indexOfTodo >= 0 && indexOfTodo < this.todos.length){
return this.todos[indexOfTodo];
} else {
return null;
}
}
clear(){
this.todos=[];
}
}

module.exports = Todo;
12 changes: 11 additions & 1 deletion 01-js/medium/countVowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
*/

function countVowels(str) {
// Your code here
// Your code here
str = str.toLowerCase();
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);

let count = 0;
for (let i = 0; i < str.length; i++) {
if (vowels.has(str.charAt(i))) {
count++;
}
}
return count;
}

module.exports = countVowels;
11 changes: 11 additions & 0 deletions 01-js/medium/palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
*/

function isPalindrome(str) {
str = str.toLowerCase().replace(/[^a-z0-9]/g, '');
let left = 0;
let right = str.length-1;
while (left < right){
if (str[left] != str[right]){
return false;
} else {
left ++;
right --;
}
}
return true;
}

Expand Down
14 changes: 12 additions & 2 deletions 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ There is no automated test for this one, this is more for you to understand time
*/

function calculateTime(n) {
return 0.01;
}
const st = new Date();
let sum = 0;
for (let i = 0 ; i <= n; i++) {
sum += i;
}
const en = new Date();
return en-st;
}

console.log(calculateTime(100));
console.log(calculateTime(100000));
console.log(calculateTime(1000000000));
20 changes: 20 additions & 0 deletions week-2/01-async-js/easy/1-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
## Create a counter in JavaScript

We have already covered this in the second lesson, but as an easy recap try to code a counter in Javascript
It should go up as time goes by in intervals of 1 second
*/
function counter() {
let count = 30;
console.log(count);
const interval = setInterval(() =>{
count--;
console.log(count);
if (count == 0){
clearInterval(interval);
console.log("CountDown Finished");
}
},1000);

}
counter();
18 changes: 18 additions & 0 deletions week-2/01-async-js/easy/2-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
## Counter without setInterval

Without using setInterval, try to code a counter in Javascript.
*/
let count = 30;
function counter() {

if (count > 0) {
console.log(count);
count--;
setTimeout(counter,1000);
} else {
console.log("count down finished !!!");
}
}

counter();
29 changes: 29 additions & 0 deletions week-2/01-async-js/easy/3-read-from-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Write code to read contents of a file and print it to the console.
You can use the fs library to as a black box, the goal is to understand async tasks.
Try to do an expensive operation below the file read and see how it affects the output.
Make the expensive operation more and more expensive and see how it affects the output.
*/
const fs = require('fs');
console.log("read from file");
let filePath = "./3-read-from-file.md";
fs.readFile(filePath,'utf-8',(err,data) => {
if (err) {
console.log(err);
} else {
console.log("data in the file is ");
console.log(data);
}
});

console.log("calc sum");

let sum = 0;
let n = 100000000;
for (let i = 0; i < n; i++) {
sum += i;
}
console.log(sum);
console.log("everything done");


18 changes: 18 additions & 0 deletions week-2/01-async-js/easy/4-write-to-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
## Write to a file
Using the fs library again, try to write to the contents of a file.
You can use the fs library to as a black box, the goal is to understand async tasks.
*/
const fs = require('fs');

let data =" The file is written using fs write";
let filePath = './4-write-to-file.md';
fs.writeFile(filePath, data, (err,success) =>{
if (err) {
console.log(err);
} else {
console.log(success);
console.log("written");
}
});
console.log("writing");
7 changes: 7 additions & 0 deletions week-2/01-async-js/hard (promises)/1-promisify-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
Write a function that returns a promise that resolves after n seconds have passed, where n is passed as an argument to the function.
*/


function wait(n) {
let p = new Promise(function(resolve){
setTimeout(resolve,n*1000);
});
return p;
}



module.exports = wait;
6 changes: 6 additions & 0 deletions week-2/01-async-js/hard (promises)/2-sleep-completely.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/

function sleep(milliseconds) {
let p = new Promise(function(resolve){
setTimeout(resolve,milliseconds);
});
return p;


}

module.exports = sleep;
17 changes: 14 additions & 3 deletions week-2/01-async-js/hard (promises)/3-promise-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@
*/

function wait1(t) {

return new Promise(function(resolve){
setTimeout(resolve,t*1000);
});
}

function wait2(t) {

return new Promise(function(resolve){
setTimeout(resolve,t*1000);
})
}

function wait3(t) {

return new Promise(function(resolve){
setTimeout(resolve,t*1000);
})
}

function calculateTime(t1, t2, t3) {
const start = Date.now();
return Promise.all([wait1(t1,),wait2(t2),wait3(t3)]).then(function(){
const end = Date.now();
return end-start;

});
}

module.exports = calculateTime;
Loading