Skip to content
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
24 changes: 23 additions & 1 deletion javascript/04/async-assignment/multisort.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs')
const fs = require('fs');

/**
* create a file sorted.txt will all the numbers from 1.txt, 2.txt and 3.txt
Expand All @@ -11,3 +11,25 @@ const fs = require('fs')
*
* NOTE: You can NOT use readFileSync or writeFileSync
*/

const getFilePath = fileName => __dirname + `/${fileName}`;
const writeFileAsync = (fileName, data) => new Promise((resolve, reject) => {
fs.writeFile(getFilePath(fileName), data, err => err ? reject(err) : resolve(true));
});
const readFileAsync = fileName => new Promise((resolve, reject) => {
fs.readFile(getFilePath(fileName), (err, data) => err ? reject(err) : resolve(data.toString()));
});

const splitByNewline = str => str.split('\n').map(el => Number(el));
const OUTPUT_FILE = 'sorted.txt';
const FILES = ['1.txt', '2.txt', '3.txt'];
const sortElementsInFiles = async () => {
const fileOneData = splitByNewline(await readFileAsync(FILES[0]));
const fileTwoData = splitByNewline(await readFileAsync(FILES[1]));
const fileThreeData = splitByNewline(await readFileAsync(FILES[2]));
const allFilesDataSorted = [...fileOneData, ...fileTwoData, ...fileThreeData].sort((a, b) => a - b);
const outputString = allFilesDataSorted.reduce((acc, currVal) => acc + currVal +'\n', '');
await writeFileAsync(OUTPUT_FILE, outputString);
};

(async () => await sortElementsInFiles())();
73 changes: 56 additions & 17 deletions javascript/04/fizzbuzz/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,61 @@ const ul = document.getElementById('fizzbuzz');
* 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz .....
*/

print.onclick = function () {
const start = Date.now();
for (let i = 1; i <= count.value; i++) {
let li = document.createElement("li");
let text = "";
if (i % 3 == 0) {
text += "fizz";
}
if (i % 5 == 0) {
text += "buzz";
}
if (text === "") {
text = i;
const FIZZ = 'fizz', BUZZ = 'buzz'
function isDivisbleBy(denominator) {
return function (num) {
return (num % denominator) === 0;
}
}

const isDivisbleBy3 = isDivisbleBy(3);
const isDivisbleBy5 = isDivisbleBy(5);

if (print) {
print.onclick = function () {
const maxCount = Number(count.value);
const elementsToPrint = [];
for (let i = 1; i <= maxCount; ++i) {
if (isDivisbleBy3(i) && isDivisbleBy5(i)) {
elementsToPrint.push(FIZZ + BUZZ);
} else if (isDivisbleBy5(i)) {
elementsToPrint.push(FIZZ);
} else if (isDivisbleBy3(i)) {
elementsToPrint.push(BUZZ);
} else {
elementsToPrint.push(i);
}
}
li.innerText = text;
ul.appendChild(li);

fizzbuzz.innerHTML = '';
elementsToPrint.forEach(ele => {
const liElement = document.createElement('li');
liElement.innerHTML = ele;
fizzbuzz.appendChild(liElement);
});
}
console.log('time = ', Date.now() - start);
};
}


/**
* Assignment Solution by Instructor (Arnav)
*/
// print.onclick = function () {
// const start = Date.now();
// for (let i = 1; i <= count.value; i++) {
// let li = document.createElement("li");
// let text = "";
// if (i % 3 == 0) {
// text += "fizz";
// }
// if (i % 5 == 0) {
// text += "buzz";
// }
// if (text === "") {
// text = i;
// }
// li.innerText = text;
// ul.appendChild(li);
// }
// console.log('time = ', Date.now() - start);
// };