var queue = [
20
];
enqueue(76);
function enqueue(transaction) {
if (queue.length === 0) {
queue.push(transaction);
return;
}
var matchExists = false;
for (var i = queue.length; i > 0; i--) {
var chosen = chooseRofN(queue, i);
for (var j = 0; j < chosen.length; j++) {
var sum = sumUp(chosen[j]);
if (sum === transaction) {
matchExists = true;
}
}
}
if (!matchExists) {
queue.push(transaction);
print(queue);
} else {
console.error('One or more combinations in the `queue` array produce the input value!');
}
}
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
function sumUp(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
function chooseRofN(items, r) {
var chosen = [];
if (r > items.length) return;
if (r === 1) {
chosen.push(items);
return chosen;
}
for (var n = 0; n < items.length; n++) {
for (var t = 0; t < items.length - 1; t++) {
var pivot = items[t];
for (var i = t + 1; i < items.length - (r - 2); i++) {
pluck = [];
pluck.push(pivot);
for (var j = 0; j < r - 1; j++) {
pluck.push(items[i + j])
}
if (!hasEqualArray(chosen, pluck)) {
chosen.push(pluck);
}
}
var temp = items[t];
items[t] = items[t + 1];
items[t + 1] = temp;
}
}
return chosen;
}
function hasEqualArray(collection, match) {
for (var i = 0; i < collection.length; i++) {
if (collection[i].length !== match.length) continue;
if (isEqual(collection[i], match)) {
return true;
}
}
return false;
}
function isEqual(arr1, arr2) {
arr1.sort(sortCallback);
arr2.sort(sortCallback);
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.