Skip to content

Promise Sort Assignment Solution 2 #4

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion javascript/02/closures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions javascript/02/practice.js
Original file line number Diff line number Diff line change
@@ -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")
19 changes: 19 additions & 0 deletions javascript/02/spread-rest.js
Original file line number Diff line number Diff line change
@@ -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))
32 changes: 32 additions & 0 deletions javascript/04/async-assignment/multisort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})