JSFiddle - React, Tailwind, and code Playground
by CommandLineDesign
JavaScript
var FindSum = function (arrayToSearch, valueToFind) {
this.arrayToSearch = arrayToSearch;
this.relevantIndexes = [];
this.findRelevantIndexes = function(){
for(var i=0; i<18; i++){
var searchIndex = i;
if(searchIndex > 9){
searchIndex = i-9;
}
var arrayPotion = this.arrayToSearch.slice(i, this.arrayToSearch.length);
if(arrayPotion.indexOf(searchIndex) > -1){
this.relevantIndexes[i] = [];
this.relevantIndexes[i].index = i;
this.relevantIndexes[i].value = searchIndex;
}
}
}
this.findRelevantIndexes();
this.valueToFind = valueToFind;
this.currentIndex = 0;
this.findSolution = function () {
while (this.currentIndex < this.relevantIndexes.length) {
var currentSolution = this.search();
if (!this.solution && currentSolution || (currentSolution && this.solution && (currentSolution[1].index < this.solution[1].index))) {
this.solution = currentSolution;
}
this.currentIndex++;
}
return this.solution;
};
};
FindSum.prototype.search = function () {
var currentValue = this.arrayToSearch[this.currentIndex];
var result = [];
result[0] = [];
result[0].value = currentValue;
result[0].index = this.currentIndex;
for (var i = this.currentIndex; i < this.relevantIndexes.length; i++) {
if (this.relevantIndexes[i] && this.relevantIndexes[i].value && currentValue + this.relevantIndexes[i].value == this.valueToFind && i != this.currentIndex) {
result[1] = [];
result[1].value = this.relevantIndexes[i].value;
result[1].index = this.relevantIndexes[i].index;
return result;
}
}
if (this.currentIndex < this.relevantIndexes.length) {
this.currentIndex++;
return this.search();
}
};
var sum_pairs = function (ints, s)...