tracks/javascript/exercises/change/example.js in trackler-2.2.1.46 vs tracks/javascript/exercises/change/example.js in trackler-2.2.1.47
- old
+ new
@@ -12,11 +12,11 @@
// data structure to hold each candidate solution that is generated
var Candidate = function () {
var searched = false;
var coins = [];
- this.Searched = function () {
+ this.searched = function () {
searched = true;
};
this.isSearched = function () {
return searched;
@@ -50,11 +50,11 @@
// fill the array with 0 to start
candidates[target] = 0;
candidates.fill(0);
// validation checks up front
- if (target == 0) return [];
+ if (target === 0) return [];
if (target < 0) {
throw new Error( 'Negative totals are not allowed.');
}
@@ -62,38 +62,22 @@
throw new Error('The total ' + target + ' cannot be represented in the given currency.');
}
initialize();
- // printAll();
-
// process the arrange until everything is searched
- while (isDone() == false) {
+ while (isDone() === false) {
let candidate = getNext();
branch(candidate);
- candidate.Searched();
+ candidate.searched();
}
- // printAll();
-
// print the result
if (typeof (candidates[target]) !== 'number') {return candidates[target].getCoins();}
throw new Error('The total ' + target + ' cannot be represented in the given currency.');
-
- // print the candidate array
- function printAll() {
- for (let j = 0; j < candidates.length; j++) {
- if (typeof (candidates[j]) === 'object') {
- console.log('index: ' + j + ' ' + candidates[j].getCoins() + ' searched: ' + candidates[j].isSearched());
- } else {
- console.log('index: ' + j + ' is empty ' + typeof (candidates[j]));
- }
- }
- }
-
// initialize the candidate array with the given coins only
function initialize() {
for (let j = 0; j < coinArray.length; j++) {
let temp = coinArray[j];
let candidate = new Candidate();
@@ -106,11 +90,11 @@
function isDone() {
let done = true;
for (let i = 0; i < candidates.length; i++) {
let temp = candidates[i];
if (typeof (temp) !== 'number') {
- if (temp.isSearched() == false) {
+ if (temp.isSearched() === false) {
done = false;
break;
}
}
}
@@ -120,10 +104,10 @@
// get the next unsearched member of the candidate array
function getNext() {
for (let i = 0; i < candidates.length; i++) {
let temp = candidates[i];
if (typeof (temp) !== 'number' &&
- temp.isSearched() == false) return temp;
+ temp.isSearched() === false) return temp;
}
return null;
}
// save a new candidate to the candidate array