aboutsummaryrefslogtreecommitdiff
path: root/challenge-134/dave-jacoby/node
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-10-11 20:39:03 -0400
committerDave Jacoby <jacoby.david@gmail.com>2021-10-11 20:39:03 -0400
commit759e2a7ed369fad02a0da434239500d4475bcbcd (patch)
tree60d6d921dab3ca036902a066f93f6f9a8b52d23c /challenge-134/dave-jacoby/node
parent38cc632e38adbe3eb4a0291a2e6f0068f27b963d (diff)
downloadperlweeklychallenge-club-759e2a7ed369fad02a0da434239500d4475bcbcd.tar.gz
perlweeklychallenge-club-759e2a7ed369fad02a0da434239500d4475bcbcd.tar.bz2
perlweeklychallenge-club-759e2a7ed369fad02a0da434239500d4475bcbcd.zip
NODE!
Diffstat (limited to 'challenge-134/dave-jacoby/node')
-rw-r--r--challenge-134/dave-jacoby/node/ch-1.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/challenge-134/dave-jacoby/node/ch-1.js b/challenge-134/dave-jacoby/node/ch-1.js
new file mode 100644
index 0000000000..47bb3a0cb9
--- /dev/null
+++ b/challenge-134/dave-jacoby/node/ch-1.js
@@ -0,0 +1,58 @@
+"use strict;";
+
+let output = [];
+pandigital(["1"]);
+for (i in output) {
+ console.log(["", output[i]].join("\t"));
+}
+
+function pandigital(state) {
+ let digits = {};
+ let numbers = [];
+ // we have the first five, we're done
+ if (output.length > 4) {
+ return;
+ }
+ // there was some duplicate issues, so we use
+ // indexOf to see if that value is in the array
+ // already
+ if (state.length == 10) {
+ let pandigit = state.join("");
+ if (output.indexOf(pandigit) === -1) {
+ output.push(pandigit);
+ return;
+ }
+ }
+ // I think I would normally prefer if
+ // for ( i in array ) would give me the value
+ // within the array not the index, but there
+ // are enough times that I want just that, so
+ // I don't need that change
+ for (let i in state) {
+ let n = state[i];
+ digits[n] = 1;
+ }
+ // the long way around getting a range
+ let range = Array(10)
+ .fill()
+ .map((n, i) => i);
+ // yes, I could've probably written a filter,
+ // but this is understandable
+ for (let i in range) {
+ if (digits[i] === undefined) {
+ numbers.push(i);
+ }
+ }
+ // the duplicates issue was related to
+ // losing track of the parens, so that
+ // this loop was inside the range loop
+ // above, but it works and I've featured
+ // the cool indexOf method, so I'll let
+ // the belt-and-suspenders solution stand.
+ for (let i in numbers) {
+ let n = numbers[i];
+ let newstate = [...state];
+ newstate.push(n);
+ pandigital(newstate);
+ }
+}