-
-
Save parzibyte/efa2a030438b7c2d4966df9bae26d2d4 to your computer and use it in GitHub Desktop.
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
const dineroAMonedas = (cantidad, monedas) => { | |
/* | |
https://parzibyte.me/blog | |
*/ | |
// Primero ordenamos las monedas de mayor a menor: | |
// https://parzibyte.me/blog/2018/10/28/ordenar-arreglos-javascript-sort/#Ordenar_arreglo_de_enteros_ascendente | |
monedas.sort((unNumero, otroNumero) => otroNumero - unNumero); | |
const monedasUsadas = []; | |
for (const moneda of monedas) { | |
while (cantidad >= moneda) { | |
monedasUsadas.push(moneda); | |
cantidad -= moneda; | |
} | |
} | |
return monedasUsadas; | |
}; | |
const cantidad = 290; | |
const billetes = [100, 50, 20]; | |
const monedasYBilletes = dineroAMonedas(cantidad, billetes); | |
console.log("Dada la cantidad %f y los billetes %s, pueden representarse con: %s", cantidad, billetes, monedasYBilletes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment