PRACTICAL NO-7
OBJECTIVE: Create a command line Utility. Using Node.Js that perform the task
related to generating a random number. Calculating the factorial of a number and
converting the text to Uppercase.
INTRODUCTION: Node.js is an open-source, cross-platform runtime environment that
allows developers to execute JavaScript code on the server side. Built on the V8 JavaScript
engine, the same engine powering Google Chrome, Node.js is known for its speed, efficiency,
and scalability. It was introduced in 2009 by Ryan Dahl and has since become a popular
choice for building web applications, APIs, and real-time services.
INPUT:
1. #!/usr/bin/env node
2.
3. import {program} from 'commander';
4.
5. program
6. .command('random')
7. .description('Generate a random number')
8. .action(() => {
9. const randomNumber = Math.random();
10. console.log(`Random Number: ${randomNumber}`);
11. });
12. program
13. .command('uppercase <string>')
14. .description('Convert a string to uppercase')
15. .action((string) => {
16. console.log(`Uppercase: ${string.toUpperCase()}`);
17. });
18.
19. program
20. .command('factorial <number>')
21. .description('Calculate the factorial of a number')
22. .action((number) => {
23. const factorial = (n) => {
24. if (n === 0) return 1;
25. return n * factorial(n - 1);
26. };
27. console.log(`Factorial: ${factorial(parseInt(number, 10))}`);
28. });
29. program.parse(process.argv);
30.
OUTPUT: