Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Created July 12, 2018 20:39
Show Gist options
  • Save mvaldesdeleon/84ef137f186a88e017b2e33643560ff3 to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/84ef137f186a88e017b2e33643560ff3 to your computer and use it in GitHub Desktop.
Highest two
class Estructura {
constructor(datos) {
this.datos = [-Infinity, -Infinity];
}
insertar(dato) {
this.datos[0] = this.datos[0] < dato ? dato : this.datos[0];
this.datos = sortPair(this.datos);
return this;
}
valores() {
return this.datos;
}
}
function sortPair(acc) {
if (acc[0] > acc[1]) {
[acc[1], acc[0]] = [acc[0], acc[1]];
}
return acc;
}
const numeros = [4, 1, 2, 3, 9, 2, 10, 22, 33, 0, 123, 415];
numeros.reduce((estructura, numero) => estructura.insertar(numero), new Estructura()).valores()
@brianfiszman
Copy link

La pasé muy bien armando esto con vos 👍

Para la linea 20 estaría genial poder asignar eso con ternario... el tema es que en JS el ternario si o si tiene que tener un else.

@mvaldesdeleon
Copy link
Author

Tendrias que repetir los valores 'tal cual' en la rama else:

[acc[1], acc[0]] = acc[0] > acc[1] ? [acc[0], acc[1]] : [acc[1], acc[0]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment