-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpasscode-checker.js
More file actions
63 lines (54 loc) · 1.21 KB
/
passcode-checker.js
File metadata and controls
63 lines (54 loc) · 1.21 KB
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
59
60
61
62
63
// start solving problem
let i = 0;
let j = 0;
let k = 0;
let l = 0;
let iter = 1;
const runLoop = (passcode) => {
// check time to complete the code
console.time("time-to-match");
while (iter <= 4) {
// view result on each iteration
console.log(l, k, j, i);
// stop loop if it matches
const result = "" + l + k + j + i;
if (passcode === result) {
console.log("matched");
break;
}
if (i < 9) i++;
else if (i === 9) {
i = 0;
if (iter === 1) iter++;
}
if (iter >= 2) {
if (j < 9 && i === 0) j++;
else if (j === 9 && i === 0) {
j = 0;
if (iter === 2) iter++;
}
}
if (iter >= 3) {
if (k < 9 && j === 0 && i === 0) k++;
else if (k === 9 && j === 0 && i === 0) {
k = 0;
if (iter === 3) iter++;
}
}
if (iter === 4) {
if (l < 9 && k === 0 && j === 0 && i === 0) l++;
else if (l === 9 && k === 0 && j === 0 && i === 0) {
l = 0;
iter++;
}
}
}
console.timeEnd("time-to-match");
};
let passcode = "9456";
// take passcode as argument
const arg = process.argv[2];
if (!isNaN(Number(arg)) && arg.length === 4) {
passcode = arg;
}
runLoop(passcode);