JSFiddle - React, Tailwind, and code Playground
by ldiego
HTML
<div>
Ingrese Numero 1: <input type="text" name="n1" id="n1" class="text"/><br/><br/>
Ingrese Numero 2: <input type="text" name="n2" id="n2" class="text"/><br/><br/>
Seleccione Operacion:<select name="operacion" id="operacion">
<option value="1">Sumar</option>
<option value="2">Restar</option>
</select><br/><br/>
Resultado: <input type="text" name="result" id="result" class="text" readOnly="readOnly"/><br/><br/>
<input type="button" value="Calcular" name="calcular" id="calcular" class="button"/>
</div>
CSS
body, input{
font-family:verdana;
font-size: 12px;
}
.text{
padding:2px;
width:500px;
}
.button{
width: 150px;
height:30px;
}
JavaScript
var lib = function(){
// VARIABLES
this.n1 = '';
this.n2 = '';
// FUNCION PARA VOLTEAR CADENAS
var reves = function(value){
return value.split('').reverse().join('');
};
// FUNCION PARA REPLICAR UNA LETRA UNA CIERTA CANTIDAD DE VECES
var replicate = function(value,n){
var result = '';
for(var i=0;i<n;i++)
result+=value;
return result;
}
// FUNCION PARA LIMPIAR ESPACIO EN BLANCO EN CADENAS
var trim = function(value){
return value.replace(/^\s+/,'').replace(/\s+$/,'');
};
// FUNCION PARA REALIZAR LA SUMA
var sumar = function(dig1,dig2,resto,i){
var suma = dig1 + dig2 + resto;
var resto = parseInt(suma/10);
var result = (suma < 10 ? suma : (i == 0 ? reves(suma.toString()) : suma % 10)).toString();
return [resto,result];
};
// FUNCION PARA REALIZAR LA RESTA
var restar = function(dig1,dig2,resto,i){
var resta = 0;
if(dig1 == 0 && resto>0){
dig1 = 10;
}
if(dig1-resto<dig2){
resta = (dig1+10-resto)-dig2;
resto = parseInt((dig1+10-resto)/10);
}else{
resta = dig1 - dig2 - resto;
if(dig1<10)resto = 0;
}
return [resto,resta.toString()];
};
// FUNCION SUMAR
this.operaciones = function(tipo){
// SE VALIDA QUE EL DATO INGRESADO SEA DE TIPO NUMERICO
if(isNaN(this.n1) || trim(this.n1) == ''){
return 'El numero uno es incorrecto';
}else if(isNaN(this.n2) || trim(this.n2) == ''){
return 'El numero dos es incorrecto';
}
// SE LIMPIA LAS CADENAS PORQUE ISNAN VALIDA QUE ES NUMERO UNA CADENA DE ESTA MANERA EJM.: " 457 "
var n1 = trim(this.n1),
n2 = trim(this.n2),
signo = '';
// DIVIDIMOS LA CADENA DE ACUERDO AL...