-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5153 from ricardo-fdz/main
Reto #38 - JavaScript
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Retos/Reto #38 - LAS SUMAS [Media]/javascript/ricardo-fdz.js
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
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); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|