- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
function tripletSum(x, a) {
found = []
copy = a.slice()
loops = 0
a.forEach((e, i) => {
loops = i
head = copy.shift()
tail = copy
tail.forEach((el, index) => {
if ( typeof tail[index+1] != 'undefined') {
console.log(`adding ${head} + (${tail[0]} + ${tail[index+1]}) = ` + parseInt(head +(tail[0] + tail[index + 1])))
sum = head + (tail[0] + tail[index + 1]);
if (sum == x) {
found.push(head, tail[0], tail[index+1])
}
}
})
// Insert head to end of tail
if (loops !=a.length) {
tail.push(head)
}
})
if (found.length) {
console.log('Found: ', found)
return true
} else {
return false
}
}
tripletSum(1107, [174, 430, 405, 706, 627, 813, 376, 94, 566, 37, 737, 142, 815, 995, 257, 653, 937, 839, 483, 356, 16, 132, 231, 842, 626, 12, 638])
/*
[5, 2, 21, 8, 4, 9]
[5] + tail[0] + tail[1] (2 + 21)
[5] + tail[0] + tail[3] (2 + 8)
head goes to end
*/