[go: up one dir, main page]

0% found this document useful (0 votes)
9 views7 pages

Web Tech Lab 7

The document outlines a Node.js command-line utility that includes functions for addition, subtraction, multiplication, division, text conversion to uppercase, factorial calculation, and random password generation. It provides a user interface to select operations and input values, executing the corresponding function based on user choice. The utility is designed to be interactive, allowing users to perform various mathematical and text operations easily.

Uploaded by

sarthakagg5678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Web Tech Lab 7

The document outlines a Node.js command-line utility that includes functions for addition, subtraction, multiplication, division, text conversion to uppercase, factorial calculation, and random password generation. It provides a user interface to select operations and input values, executing the corresponding function based on user choice. The utility is designed to be interactive, allowing users to perform various mathematical and text operations easily.

Uploaded by

sarthakagg5678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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

You might also like