gives the combination
Tue May 14 2024 19:17:01 GMT+0000 (Coordinated Universal Time)
Saved by
@Akhil_preetham
#javascript
function combinations(arr, len) {
let tempArry = []
let final = []
for (let i = 0; i < arr.length; i++) {
console.log("i ", i)
if ((arr.length - i) == (len - 1)) {
break
}
tempArry.push(arr[i])
for (let j = i + 1; j < arr.length; j++) {
console.log("j ", j)
tempArry.push(arr[j])
console.log("tempArry ", tempArry)
if (tempArry.length == len) {
console.log("tempArry inside if ", tempArry)
final.push([...tempArry])
console.log("final inside if ", final)
tempArry.pop()
}
}
tempArry = []
}
console.log("final ", final)
return final
}
combinations([1, 2, 3, 4, 5], 3)
content_copyCOPY
Comments