From f4a2ce1245cb4efef18af2b05a7dc7e9d2775b1f Mon Sep 17 00:00:00 2001 From: Marco Antonio Gallardo Date: Tue, 26 Sep 2023 22:28:24 +0200 Subject: [PATCH 1/2] reto #38 typescript --- Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts diff --git a/Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts b/Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts new file mode 100644 index 0000000000..e69de29bb2 From 8a693eccd10ae1afb65fd1f0c132ce98aace9174 Mon Sep 17 00:00:00 2001 From: Marco Antonio Gallardo Date: Wed, 27 Sep 2023 20:01:43 +0200 Subject: [PATCH 2/2] reto #38 typescript (como con los archivos adjuntos en los emails, ahora si) --- .../typescript/mgallardodev.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts b/Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts index e69de29bb2..d2400d568d 100644 --- a/Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts +++ b/Retos/Reto #38 - LAS SUMAS [Media]/typescript/mgallardodev.ts @@ -0,0 +1,26 @@ +const list: number[] = [1, 5, 3, 2]; +const target: number = 5; +//obtener todas las combinaciones +const getCombinations = (array: number[]) => { + const combinations: number[][] = [[]]; + + for (let i in array) { + const mainPositionValue = array[Number(i)]; + + const tempCombinations: number[][] = combinations.map( + (combination) => [mainPositionValue, ...combination] + ); + combinations.push(...tempCombinations); + } + return combinations; +}; +//obtener el total de los valores de un array de numeros +const totalize = (arr: number[]) => { + return arr.reduce((acc, value) => acc + value, 0); +}; + +const result = getCombinations(list).filter( + (combination) => totalize(combination) === target +); + +console.log(result);