-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolouredTriangles011.js
153 lines (114 loc) · 4.63 KB
/
colouredTriangles011.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
144
145
146
147
148
149
150
151
152
153
let row = "RGBG";
let debug = 0;
triangle(row);
function triangle(row) {
let lettersToCheck = "";
let rowLength = row.length; // so we can calculate how many times we must execute the algorithm
let result = "";
let triangleRow = [];
if (debug == 1) {
console.log(' ');
console.log('Start of the Function Triangle');
console.log('Row Input:', row);
console.log('Row Length', rowLength);
console.log(' ');
}
//Ean to row einai mono 1 gramma
if (rowLength === 1) {
triangleRow[0] = row.slice(0);
return triangleRow[0];
}
else {
let newRowLength = rowLength - 1;
triangleRow[0] = row;
if (debug == 1) {
console.log(' ');
console.log('Else');
console.log('lettersToCheck', lettersToCheck);
console.log('newRowLength', newRowLength);
console.log('Triangle 0 is ', triangleRow[0]);
console.log(' ');
}
for (let i = 0; i < rowLength - 1; i++) {
lettersToCheck = triangleRow[i];
result = '';
if (debug == 1) {
console.log(' ');
console.log(' OUTER FOR | Itteration #', i + 1);
console.log(' lettersToCheck', lettersToCheck);
console.log(' newRowLength', newRowLength);
console.log(' Triangle ', i, 'is ', triangleRow[i]);
console.log(' ');
}
for (let j = 0; j < newRowLength; j++) { //Inner For
let lettersToCheckP = lettersToCheck.slice(j, j + 2);
if (debug == 1) {
console.log(' ');
console.log(' ==INNER FOR | Before Itteration #', j + 1, '==');
console.log(' lettersToCheck', lettersToCheck);
console.log(' TriangleRow', i, 'is', triangleRow[i]);
console.log(' newRowLength', newRowLength);
console.log(' Result', result);
console.log(' --');
}
result = result + calculateLastColor(lettersToCheckP);
triangleRow[i + 1] = result;
//result = result.slice(-1); //Slice only the last character of result
if (debug == 1) {
console.log(' Inner For | After Itteration #', j + 1);
console.log(' lettersToCheck', lettersToCheck);
console.log(' Result', result);
console.log(' triangleRow ', i + 1, 'is', triangleRow[i + 1]);
console.log(' ');
}
}//Inner For
newRowLength -= 1;
if (debug == 1) {
console.log(' ');
console.log('End of OUTER FOR');
console.log('lettersToCheck', lettersToCheck);
console.log('newRowLength', newRowLength);
console.log('Result', result);
console.log(' ');
}
} //OUTER FOR
if (debug == 1) {
console.log(' ');
console.log('End of Inner For');
console.log('lettersToCheck', lettersToCheck);
console.log('newRowLength', newRowLength);
console.log('Result', result);
console.log(' ');
}
let finalArrayPossition = triangleRow.length;
if (debug == 1) {
console.log(' ');
console.log('Final Result');
console.log('lettersToCheck', lettersToCheck);
console.log('Final Triangle Row Length', finalArrayPossition);
console.log('TriangleRow', triangleRow)
console.log(' ');
}
return triangleRow[finalArrayPossition-1];
} //ELSE
} // FUNCTION TRIANGLE
function calculateLastColor(lettersToCheckInFunction) {
let digits = lettersToCheckInFunction; // The slice in this case, will select the last two digits in a string. If we put -1 will only select the last one etc https://stackoverflow.com/questions/3884632/how-to-get-the-last-character-of-a-string
if (digits.charAt(0) == digits.charAt(1)) return digits.charAt(0);
else {
switch (digits) {
case 'RG': return "B";
break;
case 'RB': return "G";
break;
case 'BR': return "G";
break;
case 'BG': return "R";
break;
case 'GR': return 'B';
break;
case 'GB': return 'R';
break;
}
}
}