-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
143 lines (118 loc) · 3.27 KB
/
script.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class CollatzFunction {
constructor(start = 2) {
this.start = start;
this.currentStep = start;
this.stepCounter = 1;
this.horizLength = 0;
this.vLength = 0;
}
// GETTERS AND SETTERS
setStart(start) {
this.start = start;
this.currentStep = start;
this.stepCounter = 1;
this.horizLength = 0;
}
getStart() {
return this.start;
}
getCurrentStep() {
return this.currentStep;
}
setHorizLength(hLength) {
this.horizLength = hLength;
}
getHorizLength() {
return this.horizLength;
}
getvLength() {
return this.vLength;
}
setvLength(vLength) {
this.vLength = vLength;
}
getStepCounter() {
return this.stepCounter;
}
// OTHER METHODS
nDivTwo() {
let quotient = this.currentStep / 2;
return quotient;
}
threeNLessOne() {
let prodSum = 3 * this.currentStep + 1;
return prodSum;
}
takeStep() {
let nextStep = this.currentStep % 2 == 0 ? this.nDivTwo() : this.threeNLessOne();
this.currentStep = nextStep;
this.stepCounter += 1;
}
addToHLength(hAdd) {
this.horizLength += hAdd;
}
}
function CollatzExecute ( s_in ) {
let start = s_in;
if ( start <= 0 ) {
start = 4 ;
}
let cc1 = new CollatzFunction(start);
let thisStep = cc1.getCurrentStep();
let lastStep = thisStep;
let str = thisStep;
while (thisStep !== 1) {
let thisLEN = String(thisStep).length;
if (thisStep % 2 === 0) {
let dent = "";
let ws = "" ;
for (let i = thisLEN - 1; i > 0; i--) {
dent += " ";
}
for (let i = cc1.getHorizLength(); i > 0; i--) {
ws += " ";
}
str +="<br>" + ws;
str += dent + "|";
str += "<br>" + ws;
} else {
str += " —— ";
cc1.addToHLength(4);
cc1.addToHLength(thisLEN);
}
lastStep = thisStep;
cc1.takeStep();
thisStep = cc1.getCurrentStep();
if (lastStep % 2 === 0) {
thisLEN = String(thisStep).length;
let lastLEN = String(lastStep).length;
let diff = lastLEN - thisLEN;
for (let i = diff; i > 0; i--) {
str += " ";
cc1.addToHLength(1);
}
}
str += thisStep;
}
ctr = "Number of Steps";
ctr += "<br>";
ctr += cc1.getStepCounter();
ctr += "<br><br>";
str = ctr + str ;
document.getElementById ( 'CC' ).style.width = cc1.getHorizLength ;
document.getElementById ( 'CC' ).innerHTML = str ;
}
let input = "" ;
function start_in () {
input = inEl.value
}
let inEl = document.getElementById ( 'in' ) ;
let bttnEl = document.getElementById ( 'button' ) ;
inEl.oninput = function() {start_in()} ;
bttnEl.onclick = function() {CollatzExecute(input)};
inEl.addEventListener("keypress", function(event) {
if (event.key === "Enter") {
event.preventDefault();
bttnEl.click();
}
});