|
| 1 | +# CLI based NodeJS Calculator |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation utilizes the `process` object, specifically its `arguments` property, to capture command line inputs. The script then processes the arguments, performing various operations based on user input. |
| 6 | + |
| 7 | +## Functionalities |
| 8 | + |
| 9 | +In this example application, users can perform basic arithmetic operations by providing command line arguments. The supported operations include addition, subtraction, multiplication, and division. |
| 10 | + |
| 11 | +- To add two numbers: |
| 12 | + ``` |
| 13 | + node app.js add num1 num2 |
| 14 | + ``` |
| 15 | + |
| 16 | +- To subtract two numbers: |
| 17 | + ``` |
| 18 | + node app.js subtract num1 num2 |
| 19 | + ``` |
| 20 | + |
| 21 | +- To multiply two numbers: |
| 22 | + ``` |
| 23 | + node app.js multiply num1 num2 |
| 24 | + ``` |
| 25 | + |
| 26 | +- To divide two numbers: |
| 27 | + ``` |
| 28 | + node app.js divide num1 num2 |
| 29 | + ``` |
| 30 | + |
| 31 | +## Implementation |
| 32 | + |
| 33 | +### Step 1: Set Up Your Project |
| 34 | + |
| 35 | +Create and open your project folder in a code editor. |
| 36 | + |
| 37 | +### Step 2: Create 'app.js' |
| 38 | + |
| 39 | +Inside this file, write the code for command line arithmetic operations. |
| 40 | + |
| 41 | +### Step 3: Process Command Line Arguments |
| 42 | + |
| 43 | +```javascript |
| 44 | +const argvs = process.argv; |
| 45 | +const argv = argvs.slice(2); |
| 46 | +``` |
| 47 | + |
| 48 | +### Step 4: Assign Arguments to Constants |
| 49 | + |
| 50 | +```javascript |
| 51 | +const operation = argv[0]; |
| 52 | +const operator1 = parseInt(argv[1]); |
| 53 | +const operator2 = parseInt(argv[2]); |
| 54 | +``` |
| 55 | + |
| 56 | +### Step 5: Perform Operations |
| 57 | + |
| 58 | +```javascript |
| 59 | +if (operation === 'add') { |
| 60 | + console.log(`${operation} is ${operator1 + operator2}`); |
| 61 | +} |
| 62 | + |
| 63 | +if (operation === 'subtract') { |
| 64 | + console.log(`${operation} is ${operator1 - operator2}`); |
| 65 | +} |
| 66 | + |
| 67 | +if (operation === 'multiply') { |
| 68 | + console.log(`${operation} is ${operator1 * operator2}`); |
| 69 | +} |
| 70 | + |
| 71 | +if (operation === 'divide') { |
| 72 | + console.log(`${operation} is ${operator1 / operator2}`); |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +Now, users can run the script with appropriate command line arguments to perform desired arithmetic operations. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +Feel free to contact me on [X formerly Twitter](https://twitter.com/mrnazu_) |
0 commit comments