Skip to content

Commit

Permalink
Added NodeJS cli calculator app
Browse files Browse the repository at this point in the history
  • Loading branch information
tadasvosylius committed Oct 2, 2022
1 parent 1a25650 commit b04427c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,9 @@ INSTITUTE/COMPANY : KLE Technological University<br>
DOMAIN/LANGUGAE : Java<br>
--------------

-----
NAME : Tadas Vosylius <br>
GITHUB : [tadasvosylius](https://github.com/tadasvosylius)<br>
INSTITUTE/COMPANY : None<br>
DOMAIN/LANGUGAE : JavaScript<br>
------
7 changes: 7 additions & 0 deletions NodeJS/cli-calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## NodeJS CLI Calculator
### Usage:
To run program, type in terminal `node app.js [operation] [arg1] [arg2]`.

Available operations: `add, substract, divide, multiply`

Example: `node app.js substract 23 13`
30 changes: 30 additions & 0 deletions NodeJS/cli-calculator/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const argv = process.argv.slice(2);
const operation = argv[0];
const operators = [];

operators[0] = parseInt(argv[1]);
operators[1] = parseInt(argv[2]);

switch (operation) {
case 'add':
outputToConsole(operation, '+', operators);
console.log('Result: ' + (operators[0] + operators[1]));
break;
case 'subtract':
outputToConsole(operation, '-', operators);
console.log('Result: ' + (operators[0] - operators[1]));
break;
case 'multiply':
outputToConsole(operation, '*', operators);
console.log('Result: ' + (operators[0] * operators[1]));
break;
case 'divide':
outputToConsole(operation, '/', operators);
console.log('Result: ' + (operators[0] / operators[1]));
break;
}

function outputToConsole(operation, operationSymbol, operators) {
console.log('Operation: ' + operation);
console.log('Condition: ' + operators[0] + ' ' + operationSymbol + ' ' + operators[1]);
}

0 comments on commit b04427c

Please sign in to comment.