Professional Documents
Culture Documents
Division: 6
Group: G-11
Code:
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
module.exports = {
add,
subtract,
multiply,
divide
};
Output:
Q2) Design a nodejs script to read a text file line by line and copy
the contents of the text to another text file.
Code:
const fs = require('fs');
function copyFile(sourceFile, destinationFile) {
const reader = fs.createReadStream(sourceFile);
const writer = fs.createWriteStream(destinationFile);
reader.setEncoding('utf8');
reader.on('data', function(chunk) {
writer.write(chunk);
});
reader.on('end', function() {
writer.end();
console.log('File copy completed.');
});
writer.on('finish', function() {
console.log('Data written to 2nd file.');
});
}
Output:
Original File:
Duplicate File: