find sum in sorted list
by Arsen
HTML
<input type="number" id="inpSum" value="1634">
<button id="btnCalc">calculate</button>
<br>
<span id="resultMsg"></span>
CoffeeScript
a=[]
for i in [0..1000]
a[i] = Math.floor 1 + Math.random() * 1000
a.sort((a,b) -> a-b)
findpos = (arr, s, i1=0, i2=arr.length) ->
curS = arr[i1] + arr[i2]
switch
when i1>=i2 then false
when s==curS then [arr[i1], arr[i2]]
when s>=curS then findpos arr, s, i1 + 1, i2
else findpos arr, s, i1, i2 - 1
elemById = (id) -> window.document.getElementById id
(elemById 'btnCalc')?.onclick = ->
sum = parseInt (elemById 'inpSum')?.value, 10
(elemById 'resultMsg')?.innerText = findpos(a, sum)