Skip to content

Commit 8611c3b

Browse files
authored
Merge pull request #108 from mrnazu/mrnazu
Mrnazu - NodeJS Calculator Implementation
2 parents e35466e + fd151d3 commit 8611c3b

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

mrnazu/README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_)

mrnazu/app.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Get command line arguments using the process object
2+
const argvs = process.argv;
3+
4+
// Extract relevant command line arguments, excluding 'node' and script file name in my case 'app.js'
5+
const argv = argvs.slice(2);
6+
7+
// Extract the operation (add, subtract, multiply, divide) from command line arguments
8+
const operation = argv[0];
9+
10+
// Parse the second and third command line arguments as integers
11+
const operator1 = parseInt(argv[1]);
12+
const operator2 = parseInt(argv[2]);
13+
14+
// Check if the operation is addition
15+
if (operation === 'add') {
16+
// Print the result of addition to the console
17+
console.log(`${operation} is ${operator1 + operator2}`);
18+
}
19+
20+
// Check if the operation is subtraction
21+
if (operation === 'subtract') {
22+
// Print the result of subtraction to the console in anotehr way
23+
console.log(`Subtraction of these numbers: ${operator1 - operator2}`);
24+
}
25+
26+
// Check if the operation is multiplication
27+
if (operation === 'multiply') {
28+
// Print the result of multiplication to the console
29+
console.log(`${operation} is ${operator1 * operator2}`);
30+
}
31+
32+
// Check if the operation is division
33+
if (operation === 'divide') {
34+
// Print the result of division to the console
35+
console.log(`${operation} is ${operator1 / operator2}`);
36+
}

0 commit comments

Comments
 (0)