PRACTICAL: 7
AIM: Build a command-line utility using [Link] that performs a specific task, such as converting
text to uppercase, calculating the factorial of a number, or generating random passwords.
THEORY:
**[Link]**
[Link] is a cross-platform, open-source runtime environment that allows JavaScript to run outside
of the browser, enabling server-side programming. Built on Google’s V8 JavaScript engine, [Link]
is known for its event-driven, non-blocking I/O model, making it efficient for building scalable and
real-time applications like APIs, chat applications, and streaming services. With a large ecosystem
of libraries available via npm (Node Package Manager), [Link] has become a popular choice for
modern web development, unifying front-end and back-end development using JavaScript.
**JavaScript**
JavaScript is a high-level, interpreted programming language widely used for creating interactive
and dynamic content on websites. It enables developers to manipulate the Document Object Model
(DOM), handle events, and add functionality like animations, form validation, and API interactions.
Originally designed as a client-side scripting language for browsers, JavaScript is now versatile,
powering back-end development through environments like [Link] and full-stack applications with
frameworks such as React, Angular, and [Link]. Its flexibility, event-driven nature, and vast
ecosystem make it a cornerstone of modern web development.
SOURCE CODE:
#!/usr/bin/env node
const readline = require('readline');
const rl = [Link]({
input: [Link],
output: [Link],
});
function toUpperCase(text) {
return [Link]();
}
function factorial(n) {
if (n < 0) return 'Factorial is not defined for negative numbers';
if (n === 0 || n === 1) return 1;
return n * factorial(n - 1);
}
function generatePassword(length) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!
@#$%^&*()'
;
let password = '';
for (let i = 0; i < length; i++) {
password += [Link]([Link]([Link]() * [Link]));
}
return password;
}
function mainMenu() {
[Link](`
Choose an option:
1. Convert text to uppercase
2. Calculate factorial of a number
3. Generate a random password
4. Exit
`);
[Link]('Enter your choice: ', (choice) => {
switch ([Link]()) {
case '1':
[Link]('Enter text to convert to uppercase: ', (text) => {
[Link](`Result: ${toUpperCase(text)}`);
mainMenu();
});
break;
case '2':
[Link]('Enter a number to calculate its factorial: ', (num) => {
const n = parseInt(num, 10);
if (isNaN(n)) {
[Link]('Please enter a valid number.');
} else {
[Link](`Factorial: ${factorial(n)}`);
}
mainMenu();
});
break;
case '3':
[Link]('Enter password length: ', (length) => {
const len = parseInt(length, 10);
if (isNaN(len) || len <= 0) {
[Link]('Please enter a valid positive number.');
} else {
[Link](`Generated Password: ${generatePassword(len)}`);
}
mainMenu();
});
break;
case '4':
[Link]('Goodbye!');
[Link]();
break;
default:
[Link]('Invalid choice. Please try again.');
mainMenu();
}
});
}
// Start the utility
mainMenu();
OUTPUT: