JSFiddle - React, Tailwind, and code Playground
by huston007
JavaScript
var MUST_USED = 6;
var groupSum6 = function(start, arr, target){
var currentStart = start;
var groupLen = 0;
var len = arr.length;
//This function count summ in array with start index passed
var recursiveSearchSumm = function(index, summ){
summ = summ || 0;
summ += arr[index];
groupLen++;
if (index+1<len && summ < target)
{
return recursiveSearchSumm(++index, summ);
} else {
return summ;
}
};
var recursiveCheckSixNotUsed = function(arr, fromIndex, sixFindIndex){
sixFindIndex = sixFindIndex || fromIndex;
if (sixFindIndex+1<len && arr[sixFindIndex]!=MUST_USED){
return recursiveCheckSixNotUsed(arr, ++sixFindIndex);
} else if (arr[sixFindIndex]==MUST_USED){
return false;
}
return true;
};
var recursiveSearchStart = function(){
groupLen = 0;
var summ = recursiveSearchSumm(currentStart);
if (summ==target){
//we only need check 6's
return recursiveCheckSixNotUsed(arr, currentStart + groupLen);
} else {
if (currentStart+1<len && arr[currentStart]!=MUST_USED){//if we found six, nothing to search more
currentStart++;
return recursiveSearchStart();
} else {
return false;
}
}
};
return recursiveSearchStart();
};
var test = function(){
console.log("Should be true:", groupSum6(0, [5, 6, 2], 8));
console.log("Should be false:", groupSum6(0, [5, 6, 2], 9));
console.log("Should be false:", groupSum6(0, [5, 6, 2], 7));
console.log("Should be true:", groupSum6(0, [1, 4, 7, 7], 5));
console.log("Should be false:", groupSum6(0, [5, 5, 3,6, 8], 8));
};
test();