From b04427ce13e44e0b7290f2df72ce1b0c0b58c6c3 Mon Sep 17 00:00:00 2001 From: Tadas Date: Sun, 2 Oct 2022 10:40:54 +0300 Subject: [PATCH] Added NodeJS cli calculator app --- CONTRIBUTING.md | 6 ++++++ NodeJS/cli-calculator/README.md | 7 +++++++ NodeJS/cli-calculator/app.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 NodeJS/cli-calculator/README.md create mode 100644 NodeJS/cli-calculator/app.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e97d019e..76e00228 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -254,3 +254,9 @@ INSTITUTE/COMPANY : KLE Technological University
DOMAIN/LANGUGAE : Java
-------------- +----- +NAME : Tadas Vosylius
+GITHUB : [tadasvosylius](https://github.com/tadasvosylius)
+INSTITUTE/COMPANY : None
+DOMAIN/LANGUGAE : JavaScript
+------ diff --git a/NodeJS/cli-calculator/README.md b/NodeJS/cli-calculator/README.md new file mode 100644 index 00000000..e4ebc792 --- /dev/null +++ b/NodeJS/cli-calculator/README.md @@ -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` \ No newline at end of file diff --git a/NodeJS/cli-calculator/app.js b/NodeJS/cli-calculator/app.js new file mode 100644 index 00000000..5e8c21cd --- /dev/null +++ b/NodeJS/cli-calculator/app.js @@ -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]); +} \ No newline at end of file