-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolouredtriangles003 - creates a correct string.js
68 lines (43 loc) · 1.86 KB
/
colouredtriangles003 - creates a correct string.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
let triangleRow = "GB";
triangle(triangleRow);
function triangle(triangleRow) {
let newStringLength = triangleRow.length;
for (let i=0; i<newStringLength; i++){
let lala = newStringLength - i;
createRow(lala, triangleRow);
}
}
function createRow(newStringLength, triangleRow) {
let finalTriangle;
let curRow = "";
let prevRow = triangleRow;
for (let i = 0; i<newStringLength-2; i++) {
curRow = curRow + createRandomColor(); //Create Random Color Line
}
let lastDigit = lastColor(prevRow);
curRow = curRow + lastDigit;
console.log(curRow);
//console.log(prevRow);
}
function lastColor(prevRow){
let curRowLength = prevRow.length;
if ( prevRow.charAt(curRowLength-1) === prevRow.charAt(curRowLength - 2)) return prevRow.charAt(curRowLength-1);
else if (prevRow.charAt(curRowLength-1) === "R" && prevRow.charAt(curRowLength-2) === "B") return "G";
else if (prevRow.charAt(curRowLength-1) === "R" && prevRow.charAt(curRowLength-2) === "G") return "B";
else if (prevRow.charAt(curRowLength-1) === "G" && prevRow.charAt(curRowLength-2) === "R") return "B";
else if (prevRow.charAt(curRowLength-1) === "G" && prevRow.charAt(curRowLength-2) === "B") return "R";
else if (prevRow.charAt(curRowLength-1) === "B" && prevRow.charAt(curRowLength-2) === "G") return "R";
else if (prevRow.charAt(curRowLength-1) === "B" && prevRow.charAt(curRowLength-2) === "R") return "G";
}
// function detectLastTwoLetters(curRow, curRowLength) {
// if (curRow[curRowLength - 1] === curRow[curRowLength - 2]) return true;
// else return false;
// }
function createRandomColor() {
let nextColorGenerator = Math.floor((Math.random() * 100) + 1);
let nextColor;
if (nextColorGenerator <= 33) nextColor = "R";
else if (nextColorGenerator > 33 && nextColorGenerator <= 66) nextColor = "G";
else nextColor = "B";
return nextColor;
}