From 9e38163aaf0eece936eec60d648ae26f7bd89119 Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Sat, 28 May 2022 00:05:07 +0530 Subject: [PATCH 1/9] added exercise-3 of week-3 assignment --- Week-3/exercise-3.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week-3/exercise-3.js diff --git a/Week-3/exercise-3.js b/Week-3/exercise-3.js new file mode 100644 index 0000000..3aa6ff0 --- /dev/null +++ b/Week-3/exercise-3.js @@ -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 From 6bbf3366bc6ce32d1d1a4e735fd51775adb8ef6a Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Sat, 28 May 2022 20:30:35 +0530 Subject: [PATCH 2/9] added exercise-1, exercise-2 and exercise-4 --- Week-3/exercise-1.js | 30 ++++++++++++++++++++++++++++++ Week-3/exercise-2.js | 39 +++++++++++++++++++++++++++++++++++++++ Week-3/exercise-4.js | 19 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 Week-3/exercise-1.js create mode 100644 Week-3/exercise-2.js create mode 100644 Week-3/exercise-4.js diff --git a/Week-3/exercise-1.js b/Week-3/exercise-1.js new file mode 100644 index 0000000..6899a2d --- /dev/null +++ b/Week-3/exercise-1.js @@ -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(); \ No newline at end of file diff --git a/Week-3/exercise-2.js b/Week-3/exercise-2.js new file mode 100644 index 0000000..0e620ab --- /dev/null +++ b/Week-3/exercise-2.js @@ -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); \ No newline at end of file diff --git a/Week-3/exercise-4.js b/Week-3/exercise-4.js new file mode 100644 index 0000000..1db5e76 --- /dev/null +++ b/Week-3/exercise-4.js @@ -0,0 +1,19 @@ + +function CreateStack() { + const items = []; + return { + + push(item) { + items.push(item); + }, + + pop() { + return items.pop(); + } + }; +} +const stack = CreateStack(); +stack.push(10); +stack.push(5); +console.log(stack.pop()); // => 5 +console.log(stack.items);// undefined From daf21fd61b385d6229b188b75e1f5c3762e8abca Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Mon, 30 May 2022 16:16:43 +0530 Subject: [PATCH 3/9] deleted additionals --- Week-3/exercise-4.js | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 Week-3/exercise-4.js diff --git a/Week-3/exercise-4.js b/Week-3/exercise-4.js deleted file mode 100644 index 1db5e76..0000000 --- a/Week-3/exercise-4.js +++ /dev/null @@ -1,19 +0,0 @@ - -function CreateStack() { - const items = []; - return { - - push(item) { - items.push(item); - }, - - pop() { - return items.pop(); - } - }; -} -const stack = CreateStack(); -stack.push(10); -stack.push(5); -console.log(stack.pop()); // => 5 -console.log(stack.items);// undefined From 1f438f48390c2fe1995428b1f3fe881201d3f606 Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Fri, 3 Jun 2022 22:40:15 +0530 Subject: [PATCH 4/9] added exercise-2.js file --- Week-4/exercise-2.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Week-4/exercise-2.js diff --git a/Week-4/exercise-2.js b/Week-4/exercise-2.js new file mode 100644 index 0000000..b3d8e41 --- /dev/null +++ b/Week-4/exercise-2.js @@ -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!'); \ No newline at end of file From c92ef7fccbb88eac78e4cbceca0d871b8d6b9ab7 Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Fri, 3 Jun 2022 23:23:17 +0530 Subject: [PATCH 5/9] added exercise-3.js file --- Week-4/exercise-3.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Week-4/exercise-3.js diff --git a/Week-4/exercise-3.js b/Week-4/exercise-3.js new file mode 100644 index 0000000..1003559 --- /dev/null +++ b/Week-4/exercise-3.js @@ -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); +} From 3a2eb677a84adcb17ad5bf7cd506a1c15b1cf45f Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Sat, 4 Jun 2022 19:35:58 +0530 Subject: [PATCH 6/9] added exercise-1.js and in that a raw pramise{} had been implemented --- Week-4/exercise-1.js | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Week-4/exercise-1.js diff --git a/Week-4/exercise-1.js b/Week-4/exercise-1.js new file mode 100644 index 0000000..af10d1f --- /dev/null +++ b/Week-4/exercise-1.js @@ -0,0 +1,55 @@ +/* +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 { + constructor (callback) { //callback is a function. + this.state = STATE.PENDING; //Initial state of Promise is empty. + this.value = undefined; + this.handlers = []; + + //Invoking the callback function by passing the presolve and the preject function of our class. + try { + callback(this.presolve, this.preject); + } catch (error) { + this.preject(error); + } + } + + //presolve() method. + presolve = (value) => {} + + //preject() method. + preject = (error) => {} + + then(onPass, onFail) { + + } + + catch(onFail) { + + } + + finally(callback) { + + } +} \ No newline at end of file From de6d7fce7a7f2940af81de64b48fa3da3dc0e2eb Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Sun, 5 Jun 2022 00:29:49 +0530 Subject: [PATCH 7/9] added the updateState() function as a part of the optimization. --- Week-4/exercise-1.js | 69 ++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/Week-4/exercise-1.js b/Week-4/exercise-1.js index af10d1f..45487fc 100644 --- a/Week-4/exercise-1.js +++ b/Week-4/exercise-1.js @@ -8,48 +8,55 @@ Exercise-4.1: 4.Should be able to accept callbacks as props. */ - function getRandomNumber() { - return Math.floor(Math.random() * 11); + return Math.floor(Math.random() * 11); } - //creating the state object. const STATE = { - PENDING: 'PENDING', - RESOLVED: 'RESOLVED', - REJECTED: 'REJECTED' -} + PENDING: "PENDING", + RESOLVED: "RESOLVED", + REJECTED: "REJECTED", +}; class Pramise { - constructor (callback) { //callback is a function. - this.state = STATE.PENDING; //Initial state of Promise is empty. - this.value = undefined; - this.handlers = []; - - //Invoking the callback function by passing the presolve and the preject function of our class. - try { - callback(this.presolve, this.preject); - } catch (error) { - this.preject(error); - } + constructor(callback) { + //callback is a function. + this.state = STATE.PENDING; //Initial state of Promise is empty. + this.value = undefined; + this.handlers = []; + + //Invoking the callback function by passing the presolve and the preject function of our class. + try { + callback(this.presolve, this.preject, time); + } catch (error) { + this.preject(error); } + } - //presolve() method. - presolve = (value) => {} + //presolve() method. + presolve = (value) => { + this.updateState(value, STATE.RESOLVED); + }; - //preject() method. - preject = (error) => {} + //preject() method. + preject = (error) => { + this.updateState(error, STATE.REJECTED); + }; - then(onPass, onFail) { + /* creating the updateState() method */ + updateState(value, state) { + setTimeout(() => { + if (this.state !== STATE.PENDING) { + return; + } - } + this.value = value; + this.state = state; + }, 0); + } - catch(onFail) { + then(onPass, onFail) {} - } - - finally(callback) { - - } -} \ No newline at end of file + catch(onFail) {} +} From 36f7ab5a62cbace6e626682112385a59d9887eb1 Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Mon, 6 Jun 2022 22:08:42 +0530 Subject: [PATCH 8/9] Implemented execute_callbacks() and then() method --- Week-4/exercise-1.js | 53 +++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/Week-4/exercise-1.js b/Week-4/exercise-1.js index 45487fc..54cc0c8 100644 --- a/Week-4/exercise-1.js +++ b/Week-4/exercise-1.js @@ -20,12 +20,13 @@ const STATE = { }; class Pramise { - constructor(callback) { - //callback is a function. - this.state = STATE.PENDING; //Initial state of Promise is empty. - this.value = undefined; - this.handlers = []; + //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.presolve, this.preject, time); @@ -35,28 +36,44 @@ class Pramise { } //presolve() method. - presolve = (value) => { + presolve(value) { this.updateState(value, STATE.RESOLVED); - }; + } //preject() method. - preject = (error) => { + preject(error) { this.updateState(error, STATE.REJECTED); - }; + } /* creating the updateState() method */ updateState(value, state) { - setTimeout(() => { - if (this.state !== STATE.PENDING) { - return; - } - - this.value = value; - this.state = state; - }, 0); + if (this.state !== STATE.PENDING) { + return; + } + this.value = value; + this.state = state; + this.execute_callbacks(); } - then(onPass, onFail) {} + 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); + if (this.catch_cb !== null) this.catch_callbacks.push(callback); + this.execute_callbacks(); + } catch(onFail) {} } From a7fe3b395f0d8f4c7727fa91a511f62e03a2705f Mon Sep 17 00:00:00 2001 From: Pradyumna-Hegde Date: Wed, 8 Jun 2022 17:15:13 +0530 Subject: [PATCH 9/9] modified then() function and added optimized code --- Week-4/exercise-1.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Week-4/exercise-1.js b/Week-4/exercise-1.js index 54cc0c8..57e229a 100644 --- a/Week-4/exercise-1.js +++ b/Week-4/exercise-1.js @@ -29,30 +29,29 @@ class Pramise { constructor(callback) { //Invoking the callback function by passing the presolve and the preject function of our class. try { - callback(this.presolve, this.preject, time); + callback(this.risolve, this.riject); } catch (error) { - this.preject(error); + this.riject(error); } } - //presolve() method. - presolve(value) { + //risolve() method. + risolve(value) { this.updateState(value, STATE.RESOLVED); } - //preject() method. - preject(error) { + //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; - } + if (this.state !== STATE.PENDING) return; + this.value = value; this.state = state; - this.execute_callbacks(); } execute_callbacks() { @@ -71,8 +70,9 @@ class Pramise { then(then_cb, catch_cb) { if (this.then_cb !== null) this.then_callbacks.push(callback); - if (this.catch_cb !== null) this.catch_callbacks.push(callback); + else this.catch_callbacks.push(callback); this.execute_callbacks(); + return new Pramise(callback); } catch(onFail) {}