From b0b0d8581de67b7aa6febda22d58fc917d864533 Mon Sep 17 00:00:00 2001 From: homerli Date: Fri, 25 Oct 2019 14:22:38 -0700 Subject: [PATCH 1/2] new code added --- JavaScript_Advance/arrow_function.js | 27 ++++++++++++++++++--------- JavaScript_Advance/event_loop.js | 12 +++++++++++- JavaScript_Advance/filtering_array.js | 17 ++++++++++++++++- JavaScript_Advance/hoisting.js | 9 ++++++++- JavaScript_Advance/promises.js | 25 +++++++++++++++++++++++-- JavaScript_Advance/while_loop.js | 7 ++++++- 6 files changed, 82 insertions(+), 15 deletions(-) diff --git a/JavaScript_Advance/arrow_function.js b/JavaScript_Advance/arrow_function.js index e63098f..cde4bf6 100644 --- a/JavaScript_Advance/arrow_function.js +++ b/JavaScript_Advance/arrow_function.js @@ -6,7 +6,7 @@ let info = { firstName: "Swapnil", lastName: "Shinde", getFullName: () => { - return(`My name is ${this.firstName} ${this.lastName}`); // Arrow functions don't have "this" property + return (`My name is ${this.firstName} ${this.lastName}`); // Arrow functions don't have "this" property } } @@ -17,7 +17,7 @@ let newInfo = { firstName: "Swapnil", lastName: "Shinde", getFullName: () => { - return(`My name is ${newInfo.firstName} ${newInfo.lastName}`); // If we are using arrow function then directly use the variables as shown + return (`My name is ${newInfo.firstName} ${newInfo.lastName}`); // If we are using arrow function then directly use the variables as shown } } @@ -39,24 +39,33 @@ console.log((new Student).getName()) // Gives error for node versions before 12. class StudentInfo { - constructor (firstName,lastName, age, branch, college){ - this.firstName = firstName; + constructor(firstName, lastName, age, branch, college) { + this.firstName = firstName; this.lastName = lastName; - this.age = age; + this.age = age; this.branch = branch; this.college = college; }; getFullName = () => { // Returns full name using string interpolation - return(`My name is ${this.firstName} ${this.lastName}`); // If we are using arrow function then directly use the variables as shown + return (`My name is ${this.firstName} ${this.lastName}`); // If we are using arrow function then directly use the variables as shown }; getBranch = () => { // Returns Branch - return(this.branch); + return (this.branch); }; } -let Swapnil = new StudentInfo("Swapnil", "Shinde",19, "Computer", "Sies"); // This way we can create new objects with arguments +let Swapnil = new StudentInfo("Swapnil", "Shinde", 19, "Computer", "Sies"); // This way we can create new objects with arguments -console.log(Swapnil.getFullName()); // Output My name is Swapnil Shinde \ No newline at end of file +console.log(Swapnil.getFullName()); // Output My name is Swapnil Shinde + +//settimeout with arrow function +setTimeout(function () { + console.log("hello world"); +}, 1000); +//another example of settimeout with arrow function +setTimeout(() => { + console.log("hello world"); +}, 0); diff --git a/JavaScript_Advance/event_loop.js b/JavaScript_Advance/event_loop.js index 99e4a14..26dcfac 100644 --- a/JavaScript_Advance/event_loop.js +++ b/JavaScript_Advance/event_loop.js @@ -4,4 +4,14 @@ setTimeout(() => { console.log("Last statement"); // This statement gets printed first -// This enables the non blocking using event based management \ No newline at end of file +// This enables the non blocking using event based management + +console.log("a"); +setTimeout(() => { + console.log("b") +}, 2000); +console.log("c"); +setTimeout(() => { + console.log("d"); +}, 0) +//except answer : a , c , d , b \ No newline at end of file diff --git a/JavaScript_Advance/filtering_array.js b/JavaScript_Advance/filtering_array.js index 08a1bf4..b33a6ba 100644 --- a/JavaScript_Advance/filtering_array.js +++ b/JavaScript_Advance/filtering_array.js @@ -2,4 +2,19 @@ const animals = ["cats", "dogs", "bunnies", "birds"]; const start_with_b = animals.filter(name => name.indexOf("b") === 0); -console.log(start_with_b); // ['bunnies', 'birds'] \ No newline at end of file +console.log(start_with_b); // ['bunnies', 'birds'] + +//function of filter (basic callback for filter) +const arr = [1, 3, 42, 2, 4, 5]; +function filter(array, callback) { + let callback_list = []; + for (let i of array) { + callback_list.push(callback(i)); + } + return callback_list; +} +//modifyable callback function +function callback(num) { + return Math.pow(num, 2); +} +console.log(filter(arr, callback)); diff --git a/JavaScript_Advance/hoisting.js b/JavaScript_Advance/hoisting.js index 1ac7ebf..eb6f91a 100644 --- a/JavaScript_Advance/hoisting.js +++ b/JavaScript_Advance/hoisting.js @@ -23,4 +23,11 @@ elem.innerHTML = x; // Display x in the element /* Conclusion :Example 1 gives the same result as Example 2 so, Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function) -*/ \ No newline at end of file +*/ + +console.log(foo); +let foo = 'foo'; //unable to print due to the hoisting behaviour + +console.log(ok); +var ok = 'this is printing'//var is hoisted + diff --git a/JavaScript_Advance/promises.js b/JavaScript_Advance/promises.js index 9fcb2c9..e3dc8f2 100644 --- a/JavaScript_Advance/promises.js +++ b/JavaScript_Advance/promises.js @@ -40,7 +40,7 @@ getAsyncDataWithPromises() .then(data => { // ... use data }).catch(e => { - // ... handle exception + // ... handle exception }); // promises promise the nodejs main thread that i'm going to come after doing a particular task @@ -77,4 +77,25 @@ asyncCall // appended with ' bar' to become 'foo bar' .then(val => `${val} bar`) // console logs 'foo bar' - .then(console.log); \ No newline at end of file + .then(console.log); + +//promise.all();\ +const fs = require('fs'); +const dir = __dirname + '/text/'; +var promisesarray = ["Text file content start after this : "]; +function readfile() { + fs.readdir(dir, 'utf-8', (err, File) => { + File.forEach(file => { + promisesarray.push(new Promise((resolve, reject) => { + fs.readFile(dir + file, 'utf-8', (err, data) => { + if (err) reject(err); + else resolve(data); + }); + })); + }); + Promise.all(promisesarray).then(data => { + console.log(data); + }) + }); +} +readfile(); \ No newline at end of file diff --git a/JavaScript_Advance/while_loop.js b/JavaScript_Advance/while_loop.js index ac99790..eaa757c 100644 --- a/JavaScript_Advance/while_loop.js +++ b/JavaScript_Advance/while_loop.js @@ -15,4 +15,9 @@ var n = 1; while (n < 6) { console.log("n is less than 6. n = " + n); n++; -} \ No newline at end of file +} + +var sum = 1; +do { + console.log(`sum now is ${sum++}`); +} while (sum <= 100); From 728df2299db0babc84fb2fba8d75ebeaec701976 Mon Sep 17 00:00:00 2001 From: Swapnil Satish Shinde <37082605+Swap76@users.noreply.github.com> Date: Sat, 26 Oct 2019 11:04:55 +0530 Subject: [PATCH 2/2] Update arrow_function.js --- JavaScript_Advance/arrow_function.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/JavaScript_Advance/arrow_function.js b/JavaScript_Advance/arrow_function.js index cde4bf6..39aea3e 100644 --- a/JavaScript_Advance/arrow_function.js +++ b/JavaScript_Advance/arrow_function.js @@ -61,11 +61,12 @@ let Swapnil = new StudentInfo("Swapnil", "Shinde", 19, "Computer", "Sies"); // T console.log(Swapnil.getFullName()); // Output My name is Swapnil Shinde -//settimeout with arrow function +//settimeout without arrow function setTimeout(function () { console.log("hello world"); }, 1000); -//another example of settimeout with arrow function + +//settimeout with arrow function setTimeout(() => { console.log("hello world"); }, 0);