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 4 #5

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
30 changes: 30 additions & 0 deletions Week-3/exercise-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* creating the memoise function */

function memoise(fn) {
const cache = new Map();

return function(...args) {
const key = args.toString();

cache.has(key) ? cache.get(key) : cache.set(key, fn(args));
return cache.get(key);
};
}


function add(a,b){
return a+b;
}

const memoiseAdd = memoise(add);

console.time();
memoiseAdd(100,100);
console.timeEnd();

memoiseAdd(100);
memoiseAdd(100, 200);

console.time();
memoiseAdd(100,100);
console.timeEnd();
39 changes: 39 additions & 0 deletions Week-3/exercise-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Using Bind.
function sum(a, b) {
return this.a + this.b;
}

const sumB = sum.bind({ a:10, b:25});

console.log(sumB()); // ==>35

/****************************************/

// Using Call
function elevate() {
var offer = [ //this == object
this.name,
'have been elevated to Manager position'
].join(' ');

console.log(offer);
}

var Arun = {
name: 'arun'
};

var Dhanush = {
name: 'dhanush'
};

elevate.call(Arun);
elevate.call(Dhanush);

/*******************************************/

// Using apply.
let numbers = [88, 64, 212, 416, 432];

let max = Math.max.apply(null, numbers);
console.log(max);
36 changes: 36 additions & 0 deletions Week-3/exercise-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function createIncrement() {
let count = 0;

function increment() {
count++;
}

let message = `count is ${count}`;

function log() {
console.log(message);
}

return [increment, log];
}

//calling the function.
const [increment, log] = createIncrement();
/* 1.variable count is created and initialized with a value: 0,
2. function increment() is created but not called,
3. String message is created (at this stage, since function increment() is not yet called, the body part of that increment() function is not executed) as message = "count is 0";,
4. function log() is created but not called,
5. returns both increment() and log() functions.
*/

increment(); // value of the count is incremented to count: 1.
increment(); // value of the count is incremented to count: 2.
increment(); // value of the count is incremented to count: 3.
log();
/*
according to the instructions written in the body part of the log()
function, it should display the value of the message in the console and if you see the line-21, the value of the message is "count is 0" and is displayed as 'count is 0'.
*/


// Answer: count is 0
79 changes: 79 additions & 0 deletions Week-4/exercise-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Exercise-4.1:
Implement a function named 'getNumber' which generatesa random number. If randomNumberis divisible by 5 it will reject the promise else it will resolve the promise. Let’s also keep thepromise resolution/rejection time as a variable.

1.JS promises should not be used.
2.A custom promise function should be created.
3.This function should be able to handle all 3 states Resolve, Reject and Fulfilled.
4.Should be able to accept callbacks as props.
*/

function getRandomNumber() {
return Math.floor(Math.random() * 11);
}

//creating the state object.
const STATE = {
PENDING: "PENDING",
RESOLVED: "RESOLVED",
REJECTED: "REJECTED",
};

class Pramise {
//callback is a function.
state = STATE.PENDING; //Initial state of Promise is empty.
value = undefined;
then_callbacks = [];
catch_callbacks = [];

constructor(callback) {
//Invoking the callback function by passing the presolve and the preject function of our class.
try {
callback(this.risolve, this.riject);
} catch (error) {
this.riject(error);
}
}

//risolve() method.
risolve(value) {
this.updateState(value, STATE.RESOLVED);
}

//riject() method.
riject(error) {
//error is usually an Object (eg: new Error('message');)
this.updateState(error, STATE.REJECTED);
}

/* creating the updateState() method */
updateState(value, state) {
if (this.state !== STATE.PENDING) return;

this.value = value;
this.state = state;
}

execute_callbacks() {
if (this.state === STATE.RESOLVED) {
this.then_callbacks.forEach((callback) => {
callback(this.value);
});
this.then_callbacks = [];
} else {
this.then_callbacks.forEach((callback) => {
callback(this.value);
});
this.then_callbacks = [];
}
}

then(then_cb, catch_cb) {
if (this.then_cb !== null) this.then_callbacks.push(callback);
else this.catch_callbacks.push(callback);
this.execute_callbacks();
return new Pramise(callback);
}

catch(onFail) {}
}
24 changes: 24 additions & 0 deletions Week-4/exercise-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Person {
constructor (name) {
this.name = name;
}

getName() {
return this.name;
}

setName(name) {
if(typeof name !== 'string') {
throw new Error(`Dhyaaaaan De!`);
} else this.name = name;
}
}

class Teacher extends Person {
teach(subject) {
console.log(`${this.name} is now teaching ${subject}`);
}
}

const ajeesh = new Teacher('Ajeesh');
ajeesh.teach('Mathematics!');
20 changes: 20 additions & 0 deletions Week-4/exercise-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Fibonacci = {
number: 5,
[Symbol.iterator]: function () {
let i = 1,
older = 0,
newer = 0;
return {
next: () => {
if (i++ <= this.number) {
[older, newer] = [newer, older + newer || 1];
return { value: older, done: false };
} else return { done: true };
},
};
},
};

for (let num of Fibonacci) {
console.log(num);
}