Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 20, 2021 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/b11f693e12a2d33c8f62bc3e0152b788 to your computer and use it in GitHub Desktop.
Save parzibyte/b11f693e12a2d33c8f62bc3e0152b788 to your computer and use it in GitHub Desktop.
class FraccionMixta {
/*
https://parzibyte.me/blog
*/
constructor(entero, fraccion) {
this.entero = entero;
this.fraccion = fraccion;
}
aImpropia() {
let numerador = this.fraccion.numerador;
if (this.entero) {
numerador = numerador + (this.fraccion.denominador * this.entero);
}
return new Fraccion(numerador, this.fraccion.denominador);
}
static desdeImpropia(fraccion) {
let entero = 0;
if (fraccion.numerador >= fraccion.denominador) {
entero = Math.floor(fraccion.numerador / fraccion.denominador);
const residuo = fraccion.numerador % fraccion.denominador;
if (residuo > 0) {
fraccion = new Fraccion(residuo, fraccion.denominador);
} else {
fraccion = null;
}
}
return new FraccionMixta(entero, fraccion);
}
toString() {
let resultado = "";
if (this.entero) {
resultado = resultado.concat(this.entero);
if (this.fraccion) {
resultado = resultado.concat(" + ");
}
}
if (this.fraccion) {
resultado = resultado.concat(this.fraccion.toString());
}
return resultado;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment