Skip to content

Commit

Permalink
Merge pull request #5153 from ricardo-fdz/main
Browse files Browse the repository at this point in the history
Reto #38 - JavaScript
  • Loading branch information
Roswell468 authored Sep 30, 2023
2 parents 880626a + 6c44a50 commit 2820a23
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Retos/Reto #38 - LAS SUMAS [Media]/javascript/ricardo-fdz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

const convertToBinary = (num)=>{
let binary = []
while (num!==0){
let remainder = parseInt(num%2)
num = Math.floor(num/2);
binary.push(remainder);
}
return binary
}

const getSumsArray=(array, target )=>{
let sumsArray = [];
const combinations = Math.pow(2,array.length);
console.log(combinations);
for (var i = 1; i < combinations; i++) {
let positions = convertToBinary(i);
positions = positions.map((pos,index)=>pos!==0? index:null)
positions = positions.filter(pos=>pos !== null)
let currentSum = 0;
let curreSumArray= [];
positions.forEach(pos=>{
currentSum += array[pos];
curreSumArray.push(array[pos]);
})
if(currentSum===target){
sumsArray.push(curreSumArray)
}
}
return sumsArray
}

let res = getSumsArray([1, 5, 3, 2,1],7);
console.log(res);







0 comments on commit 2820a23

Please sign in to comment.