From 39daa5899800d21542cfa57e4b7b445708a5a74d Mon Sep 17 00:00:00 2001 From: Prasanna Date: Wed, 7 Dec 2022 13:07:30 +0530 Subject: [PATCH 1/2] Added Practicce files --- javascript/02/closures.js | 2 +- javascript/02/practice.js | 30 ++++++++++++++++++++++++++++++ javascript/02/spread-rest.js | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 javascript/02/practice.js create mode 100644 javascript/02/spread-rest.js diff --git a/javascript/02/closures.js b/javascript/02/closures.js index dacb842..b2701ef 100644 --- a/javascript/02/closures.js +++ b/javascript/02/closures.js @@ -16,7 +16,7 @@ console.log(c1(), c1(), c1()) console.log(c2(), c2(), c2()) console.log(c1(), c1(), c1()) - +// Shadowing of variables function a () { let x = 10 let p = 1 diff --git a/javascript/02/practice.js b/javascript/02/practice.js new file mode 100644 index 0000000..fc84deb --- /dev/null +++ b/javascript/02/practice.js @@ -0,0 +1,30 @@ +// If the object binded to the function is changed inside the function, then does it also chenge in main code. ans: Yes + +console.log(this) // blank object + +function fun1() { + this.a = "lavde" + console.log(this) // globalThis +} + +let obj2 = { + a: "hell", + b:10, + c: true, + d: fun1 +} + +obj2.d() + +let obj = { + a: "hello", + b: 10, + c: true +} + +// let f = fun1.bind(obj); +// f(); +// fun1.call(obj) +fun1.apply(obj) + +console.log(obj, "Original Object") \ No newline at end of file diff --git a/javascript/02/spread-rest.js b/javascript/02/spread-rest.js new file mode 100644 index 0000000..a0aec1f --- /dev/null +++ b/javascript/02/spread-rest.js @@ -0,0 +1,19 @@ +// Spread operator - Converts array to individual values +function sumOne(a, b) { + return a+b +} + +arr = [5, 4] +console.log(sumOne(...arr)) + +// Rest operator - Converts individual values to array +function calSum(a, b, ...args) { + let multi = a*b + let sum = 0 + for (const arg of args) { + sum += arg + } + return [multi, sum] +} + +console.log(calSum(3, 7, 34, 2)) \ No newline at end of file From 126577f854acde71dd837b08603890602a4a7ac7 Mon Sep 17 00:00:00 2001 From: Prasanna gramopadhye <69106022+gramo37@users.noreply.github.com> Date: Wed, 21 Dec 2022 12:49:56 +0530 Subject: [PATCH 2/2] Promise Sort Assignment Solution Create a file sorted.txt will all the numbers from 1.txt, 2.txt and 3.txt --- javascript/04/async-assignment/multisort.js | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/javascript/04/async-assignment/multisort.js b/javascript/04/async-assignment/multisort.js index c2eade5..97f6ad9 100644 --- a/javascript/04/async-assignment/multisort.js +++ b/javascript/04/async-assignment/multisort.js @@ -11,3 +11,35 @@ const fs = require('fs') * * NOTE: You can NOT use readFileSync or writeFileSync */ + +const fileNames = ['/1.txt', '/2.txt', '/3.txt'] +const arrOfPromises = [] + +function mergeAllIntoOne(arrs) { + let ans = []; + arrs.forEach(element => { + ans = [...ans, ...element] + }); + ans.sort((a, b) => a - b); + return (ans); +} + +fileNames.forEach(filename => { + let promise = new Promise((resolve, reject) => { + fs.readFile(__dirname + filename, function (err, data) { + if (err) throw err; + let array = data.toString().split("\n"); + var numberArray = array.map(Number); + numberArray.sort((a, b) => a - b) + resolve(numberArray); + }) + }) + arrOfPromises.push(promise); +}) + +Promise.all(arrOfPromises).then((arrs) => { + var file = fs.createWriteStream(__dirname + '/sorted.txt'); + file.on('error', function (err) { throw err }); + mergeAllIntoOne(arrs).forEach(function (v) { file.write(v + '\n'); }); + file.end(); +})