Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
86 lines (84 loc) · 2.48 KB
/
Copy pathscript.js
File metadata and controls
86 lines (84 loc) · 2.48 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Calculadora decimal a cualquier base explicando los pasos para llegar al resultado
* Hecho con Vue.JS 2 + Bulma CSS
* @author parzibyte
*/
const BINARIO = 2, OCTAL = 8, HEXADECIMAL = 16;
Vue.use(Toasted);
const app = new Vue({
el: '#app',
data: () => ({
bases: [BINARIO, OCTAL, HEXADECIMAL],
baseSeleccionada: BINARIO,
numeroDecimal: null,
operaciones: [],
resultado: null,
}),
mounted() {
this.enfocarInput();
},
methods: {
enfocarInput() {
this.$refs.inputPrincipal.focus();
},
obtenerEquivalencia(digitoDecimal) {
if (this.baseSeleccionada === BINARIO || this.baseSeleccionada === OCTAL) {
return digitoDecimal;
}
if (this.baseSeleccionada === HEXADECIMAL) {
return {
0: "0",
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "A",
11: "B",
12: "C",
13: "D",
14: "E",
15: "F",
}[digitoDecimal];
};
},
resetear() {
this.operaciones = [];
},
calcular() {
this.resetear();
if (!this.numeroDecimal) {
return;
}
let decimal = this.numeroDecimal;
let resultado = "";
while (decimal > 0) {
let residuo = decimal % this.baseSeleccionada;
let anteriorDecimal = decimal;
decimal = Math.floor(decimal / this.baseSeleccionada);
let equivalencia = this.obtenerEquivalencia(residuo);
const operacion = {
residuo, decimal, anteriorDecimal,
};
if (this.baseSeleccionada === HEXADECIMAL && equivalencia != residuo) {
operacion.equivalencia = equivalencia;
}
this.operaciones.push(operacion);
resultado = equivalencia + resultado;
}
this.resultado = resultado;
}
},
watch: {
numeroDecimal() {
this.calcular();
},
baseSeleccionada() {
this.enfocarInput();
}
},
});