Skip to content
Merged
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
28 changes: 19 additions & 9 deletions JavaScript_Advance/arrow_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -39,24 +39,34 @@ 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
console.log(Swapnil.getFullName()); // Output My name is Swapnil Shinde

//settimeout without arrow function
setTimeout(function () {
console.log("hello world");
}, 1000);

//settimeout with arrow function
setTimeout(() => {
console.log("hello world");
}, 0);
12 changes: 11 additions & 1 deletion JavaScript_Advance/event_loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ setTimeout(() => {

console.log("Last statement"); // This statement gets printed first

// This enables the non blocking using event based management
// 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
17 changes: 16 additions & 1 deletion JavaScript_Advance/filtering_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
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));
9 changes: 8 additions & 1 deletion JavaScript_Advance/hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
*/

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

25 changes: 23 additions & 2 deletions JavaScript_Advance/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,4 +77,25 @@ asyncCall
// appended with ' bar' to become 'foo bar'
.then(val => `${val} bar`)
// console logs 'foo bar'
.then(console.log);
.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();
7 changes: 6 additions & 1 deletion JavaScript_Advance/while_loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ var n = 1;
while (n < 6) {
console.log("n is less than 6. n = " + n);
n++;
}
}

var sum = 1;
do {
console.log(`sum now is ${sum++}`);
} while (sum <= 100);