Created
July 12, 2018 20:39
-
-
Save mvaldesdeleon/84ef137f186a88e017b2e33643560ff3 to your computer and use it in GitHub Desktop.
Highest two
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
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
.