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

week-2 assignments completed #1056

Open
wants to merge 2 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
19 changes: 18 additions & 1 deletion 01-js/easy/anagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@
*/

function isAnagram(str1, str2) {

return sort_str(str1)===sort_str(str2)
}
function sort_str(str){
str = str.toLowerCase()
let sorted_str = "0"
for(let i=0;i<str.length;i++){
for(let j=0;j<sorted_str.length;j++){
if(sorted_str[j]>str[i]){
sorted_str = sorted_str.substring(0,j)+str[i]+sorted_str.substring(j)
break
}
if(j==sorted_str.length-1) {
sorted_str+= str[i]
break
}
}
}
return sorted_str
}

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

function calculateTotalSpentByCategory(transactions) {
return [];
let ans=[];
for(let i=0;i<transactions.length;i++){
let obj1 = transactions[i]
let b= false
let index=-1;
for(let j=0;j<ans.length;j++){
if(ans[j].category==obj1.category){
b= true;
index = j;
break;
}
}
if(!b){
let obj2 = { "category" : obj1.category, "totalSpent" : obj1.price}
ans.push(obj2);
}
else{
ans[index].totalSpent += obj1.price;
}
}
return ans;
}

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

function findLargestElement(numbers) {

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

module.exports = findLargestElement;
29 changes: 28 additions & 1 deletion 01-js/hard/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@
Once you've implemented the logic, test your code by running
*/

class Calculator {}
class Calculator {
constructor(){
this.result=0;
}
add(num){
this.result += num;
}
subtract(num){
this.result -= num;
}
multiply(num){
this.result *= num;
}
divide(num){
if(num!=0) this.result /= num;
else throw ErrorEvent;
}
clear(){
this.result =0;
}
getResult(){
return this.result ;
}
calculate(expression){
this.result = (eval(expression))
if (this.result==Infinity) throw ErrorEvent;
}
}

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

class Todo {

constructor(){
this.todos =[] ;
}

add(todo){
this.todos.push(todo);
}
remove(index){
if(index<this.todos.length && index>=0) this.todos.splice(index,1);
}
update(index ,updatedTodo){
if(index<this.todos.length && index>=0) this.todos[index] = updatedTodo;
}
get(index){
let r= this.todos[index];
if(r) return r;
else return null;
}
getAll(){
return this.todos;
}
clear(){
this.todos = [];
}
}

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

function countVowels(str) {
// Your code here
str= str.toLowerCase();
let vowels = "aeiou";
let count=0;
for(let i=0;i<str.length;i++) if(vowels.indexOf(str.charAt(i))>=0) count++;
return count;
}

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

function isPalindrome(str) {
let str2= str.toLowerCase();
str=""
for(let i=0;i<str2.length;i++) if(str2[i]>='a'&& str2[i]<='z') str+=str2[i];
let n= str.length -1;
for(let i=0;i<n/2;i++) if(!(str[i]==str[n-i])) return false;
return true;
}

Expand Down
8 changes: 7 additions & 1 deletion 01-js/medium/times.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ There is no automated test for this one, this is more for you to understand time
*/

function calculateTime(n) {
return 0.01;
let num=0;
let date = new Date();
console.log(date.getTime())
for(let i=0;i<n;i++) num+= i;
let date2 = new Date();
console.log((date2.getTime()-date.getTime())/1000);
return num;
}
13 changes: 13 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,13 @@
// ## 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

let num=0;

function r(){
num+=1
console.log(num);
}

setInterval(r,1000)
4 changes: 0 additions & 4 deletions week-2/01-async-js/easy/1-counter.md

This file was deleted.

13 changes: 13 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,13 @@
// ## Counter without setInterval
// Without using setInterval, try to code a counter in Javascript. There is a hint at the bottom of the file if you get stuck.
// (Hint: setTimeout)

let num =0;

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

counter()
76 changes: 0 additions & 76 deletions week-2/01-async-js/easy/2-counter.md

This file was deleted.

13 changes: 13 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,13 @@
// ## Reading the contents of a file

// 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');

fs.readFile('./week-2/01-async-js/easy/a.txt',(err,data)=>{
for(let i=0;i<10000000000;i++){};
console.log(data.toString());
})
7 changes: 0 additions & 7 deletions week-2/01-async-js/easy/3-read-from-file.md

This file was deleted.

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');

async function k(){
fs.readFile("./week-2/01-async-js/easy/a.txt",(err,data)=>{
console.log(data.toString());
fs.writeFile("./week-2/01-async-js/easy/a.txt","this file is written by write-to-file.js file",()=>{
fs.readFile("./week-2/01-async-js/easy/a.txt",(err,data)=>{
console.log(data.toString());
})
})
})
}

k()
3 changes: 0 additions & 3 deletions week-2/01-async-js/easy/4-write-to-file.md

This file was deleted.

1 change: 1 addition & 0 deletions week-2/01-async-js/easy/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
most beautiful file
5 changes: 4 additions & 1 deletion week-2/01-async-js/hard (promises)/1-promisify-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/

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

module.exports = wait;
8 changes: 7 additions & 1 deletion week-2/01-async-js/hard (promises)/2-sleep-completely.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
* the function should return a promise just like before
*/

function sleep(milliseconds) {
async function sleep(milliseconds) {
let p = await new Promise( (reslove)=>{
setTimeout(reslove,milliseconds);
}
)
return p;

}

module.exports = sleep;
Loading