WEB TECH LAB ASSIGNMENT-7
Build a command-line utility using Node.js that performs a specific task, such as
- Make an external js file that have 4 functions performing addition, subtraction,
division and multiplication.
- converting text to uppercase, -
calculating the factorial of a number, -
or generating random passwords.
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) { return
'Error: Division by zero is not allowed.'; }
return a / b;
function toUpperCase(text) {
return text.toUpperCase();
function factorial(num) { if (num < 0) return 'Error: Factorial is not defined
for negative numbers'; if (num === 0 || num === 1) return 1; return num
* factorial(num - 1);
function generatePassword(length) {
const charset =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()';
let password = ''; for (let i = 0; i < length; i++) { const
randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
return password;
}
module.exports = {
add,
subtract,
multiply,
divide,
toUpperCase,
factorial,
generatePassword
};
const readline = require('readline'); const
functions = require('./utility');
const rl = readline.createInterface({
input: process.stdin, output:
process.stdout
});
console.log('Welcome to the Node.js Utility!');
console.log('Choose an operation:'); console.log('1.
Addition');
console.log('2. Subtraction'); console.log('3.
Multiplication'); console.log('4. Division');
console.log('5. Convert text to uppercase');
console.log('6. Calculate factorial');
console.log('7. Generate random password');
rl.question('Enter your choice (1-7): ', (choice) => {
switch (choice) {
case '1':
rl.question('Enter two numbers separated by space: ', (input) => {
const [num1, num2] = input.split(' ').map(Number);
console.log(`Result: ${functions.add(num1, num2)}`);
rl.close();
});
break;
case '2':
rl.question('Enter two numbers separated by space: ', (input) => {
const [num1, num2] = input.split(' ').map(Number);
console.log(`Result: ${functions.subtract(num1, num2)}`);
rl.close();
});
break;
case '3':
rl.question('Enter two numbers separated by space: ', (input) => {
const [num1, num2] = input.split(' ').map(Number);
console.log(`Result: ${functions.multiply(num1, num2)}`);
rl.close();
});
break;
case '4':
rl.question('Enter two numbers separated by space: ', (input) => {
const [num1, num2] = input.split(' ').map(Number);
console.log(`Result: ${functions.divide(num1, num2)}`);
rl.close();
});
break;
case '5':
rl.question('Enter text to convert to uppercase: ', (text) => {
console.log(`Result: ${functions.toUpperCase(text)}`);
rl.close();
});
break; case
'6':
rl.question('Enter a number to calculate its factorial: ', (num) => {
const result = functions.factorial(Number(num));
console.log(`Result: ${result}`); rl.close();
});
break;
case '7':
rl.question('Enter the desired length of the password: ', (length) => {
const password = functions.generatePassword(Number(length));
console.log(`Generated password: ${password}`);
rl.close();
});
break;
default:
console.log('Invalid choice, please select a valid option (1-7).');
rl.close();
break;
});
OUTPUT:
Name-Utkarsh Tyagi
Class- V-C
Roll number- 48
University Roll Number-2200290100181