oj leetcode twoSum solution
probablyworks
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore.js"></script>
<div id="result"></div>
<button >reroll</button>
JavaScript
function twoSum(intArray, target) {
var resultIndexSet = []
intArray.forEach(function (item, index, set) {
var pairItemIndex;
if (item >= target) {
return;
} else if (resultIndexSet.length == 2) {
return;
} else {
pairItemIndex = intArray.indexOf(target - item);
if (pairItemIndex !== -1) {
console.log(item, pairItemIndex, item + intArray[pairItemIndex])
resultIndexSet.push(index + 1, pairItemIndex + 1)
}
}
});
return resultIndexSet;
}
window.roll = function() {
var arr = [];
for (var i = 0; i < 10000; i++) {
arr.push(Math.ceil((Math.random() * 10) + 1));
}
console.log(arr)
return arr;
}
$('button').click(function() {
$('#result').append(twoSum(roll(), 200).toString() + '<br>')
});