-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
68 lines (63 loc) · 2.07 KB
/
Copy pathapp.js
File metadata and controls
68 lines (63 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//variables
let x = 1;
let y = 2;
let operation = "entrar"; //Values: "suma", "resta", "multiplicación", and "división".
//function
function realizarOperacion(num1, num2, operacion)
{
switch(operacion)
{
case "suma":
return num1 + num2;
case "resta":
return num1 - num2;
case "multiplicación":
return num1 * num2;
default:
return num1 / num2;
}
}
function validMathOperation()
{
if(operation === "suma" || operation === "resta" || operation === "multiplicación" || operation === "división")
{
return true;
}
return false;
}
//loop for multiple operations - main function
do
{
//greetings and introduction
alert("Bienvenido al programa de calculadora aritmética básica para 2 números.");
alert("Las opciones disponibles son:\nSuma\nResta\nMultiplicación\nDivisión\nSalir");
//ask for the option
operation = prompt("Ingrese el nombre de la opción que quiere realizar: ");
operation = operation.toLowerCase();
//call of the validation function
if(validMathOperation())
{
//user input
x = Number(prompt("Ingrese el valor del primer número: "));
y = Number(prompt("Ingrese el valor del segundo número: "));
//check division by zero
if(operation === "división" && y === 0)
{
alert("ERROR: la división entre 0 no está definida.");
}
else
{
//call of the main function
alert("El resultado de la operación es: " + realizarOperacion(x,y,operation));
//perform another operation?
//alert("¿Quiere realizar otra operación? De ser 'si' su respuesta, ingrese el nombre de la operación. De lo contrario, escriba 'salir'.");
//operation = prompt("Próxima acción a realizar: ");
}
}
else if(operation !== "salir")
{
alert("ERROR: Opción u operación no valida. Por favor, reintentar nuevamente.");
}
}while(operation !== "salir")
//Farewell message
alert("¡Hasta luego!");