Skip to content

Instantly share code, notes, and snippets.

@urielhdz
Created May 30, 2019 19:18
Show Gist options
  • Save urielhdz/6b806359744d7339372c8fdde9a9954e to your computer and use it in GitHub Desktop.
Save urielhdz/6b806359744d7339372c8fdde9a9954e to your computer and use it in GitHub Desktop.
Calculator in NodeJS terminal
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();
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")
@matiasmartin11
Copy link

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