aboutsummaryrefslogtreecommitdiff
path: root/challenge-134/dave-jacoby/node/ch-1.js
blob: 47bb3a0cb94febeb0c04c3cd7f45f623ead78671 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
  }
}