Created
May 30, 2019 19:18
-
-
Save urielhdz/6b806359744d7339372c8fdde9a9954e to your computer and use it in GitHub Desktop.
Calculator in NodeJS terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const interface = readline.createInterface({ input: process.stdin, output: process.stdout }); | |
const question = require('./question')(interface); | |
const operaciones = { | |
"1": { | |
text: "Suma de dos números", | |
method: (n1, n2) => n1 + n2 | |
}, | |
"2": { | |
text: "Resta de dos números", | |
method: (n1, n2) => n1 - n2 | |
}, | |
"3": { | |
text: "Multiplicación de dos números", | |
method: (n1, n2) => n1 * n2 | |
}, | |
"4": { | |
text: "División de dos números", | |
method: (n1, n2) => n1 / n2 | |
}, | |
"5": { | |
text: "Salir" | |
} | |
}; | |
async function main(){ | |
console.log('\033[2J'); | |
for (let clave in operaciones) { | |
console.log(`${clave}.- ${operaciones[clave]["text"]}`); | |
} | |
let response = await question("Qué operación deseas realizar, escribe el número: "); | |
if(response == "5"){ | |
interface.close(); | |
return; | |
} | |
let firstNumber = parseInt(await question("Dame el primer número: ")); | |
let secondNumber = parseInt(await question("Dame el segundo número: ")); | |
let result = operaciones[response]["method"](firstNumber,secondNumber); | |
console.log(`El resultado es: ${result}`); | |
await interface.question("Presiona Enter para continuar", () => main()); | |
} | |
main(); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const interface = require('readline-sync'); | |
let response = ""; | |
do{ | |
console.log("1.- Suma"); | |
console.log("2.- Resta"); | |
console.log("3.- Multiplicación"); | |
console.log("4.- Divisón"); | |
console.log("5.- Salir"); | |
response = interface.question("Qué operación deseas realizar, escribe el número: "); | |
if(response != "5"){ | |
let firstNumber = interface.question("Dame el primer número: "); | |
let secondNumber = interface.question("Dame el segundo número: "); | |
switch (response) { | |
case "1": | |
var result = parseInt(firstNumber) + parseInt(secondNumber); | |
console.log("El resultado es: "+result); | |
break; | |
case "2": | |
var result = parseInt(firstNumber) - parseInt(secondNumber); | |
console.log("El resultado es: " + result); | |
break; | |
case "3": | |
var result = parseInt(firstNumber) * parseInt(secondNumber); | |
console.log("El resultado es: " + result); | |
break; | |
case "4": | |
var result = parseInt(firstNumber) / parseInt(secondNumber); | |
console.log("\n\n El resultado es: " + result + "\n\n"); | |
break; | |
} | |
} | |
}while(response != "5") | |
Program execution finishes when the result is given.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//node v16.13.1
const readline = require('readline');
const { stdin: input, stdout: output } = require('process');
const leer = readline.createInterface({ input, output });
const util = require('util');
const question = util.promisify(leer.question).bind(leer);
//menu
const operaciones = {
"1": {
text: "Suma",
method: (x, y) => x + y
},
"2": {
text: "Resta",
method: (x, y) => x - y
},
"3": {
text: "Multiplicación",
method: (x, y) => x * y
},
"4": {
text: "División",
method: (x, y) => x / y
},
"5": {
text: "Salir"
}
};
async function main(){
//mostrar menu
for (let i in operaciones) {
console.log(
${i}.- ${operaciones[i]["text"]}
);}
let response = await question("Qué operación deseas realizar, escribe el número: ");
if(response == "5"){
leer.close();
return;
}
let n1 = parseInt(await question("primer número: "));
let n2 = parseInt(await question("segundo número: "));
let result = operaciones[response]"method";
console.log(
El resultado es: ${result}
);await question("Presiona Enter para continuar", () => main());
leer.close();
}
main();