"use strict" var Change = function() { } module.exports=Change; //data structure to hold each candidate solution that is generated var Candidate = function() { var searched=false; var coins=[]; this.Searched =function() { searched=true; } this.isSearched = function() { return searched; } this.getCoins = function() { return coins; } this.addCoin = function(coin) { function sortNum(a,b) { return a-b; } coins.push(coin); coins.sort(sortNum); } this.getCoinCount=function() { return coins.length; } this.getSum=function() { function getSum(total, num){return total+num;} return coins.reduce(getSum); } } Change.prototype.calculate=function(coinArray, target) { var candidates=[]; //fill the array with 0 to start candidates[target]=0; candidates.fill(0); //validation checks up front if(target==0)return []; if(target<0) { throw new Error( 'Negative totals are not allowed.'); } if(targetcandidate.getCoinCount()) { candidates[sum]=candidate; } if(candidate.getSum()<=target && typeof(candidates[sum])=='number') { candidates[sum]=candidate; } } //for the candidate, generate another candate for each of the possible coins function branch(current) { for(let j=0; j