-
Notifications
You must be signed in to change notification settings - Fork 1
/
typeWriter.js
209 lines (160 loc) · 4.49 KB
/
typeWriter.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// requirejs(['typeWriter.js']);
// let {grid, n} = require('./fastGrid.js')
// console.log(grid,n);
var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusm"
var speed = 5;
// var ele = document.getElementById("select");
var type = ""
//implement
var text2 = ["Lorem ipsum dolor sit amet, consectetur adipisicing elit,",
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nos",
"trud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."]
var n = 9
var randomNumber = (n) => {
return Math.floor(Math.random() * n + 1);
};
//creating a Random row of n length
var ran = () => {
const arr = [];
while (arr.length < n) {
const x = randomNumber(n);
if (!arr.includes(x)) arr.push(x);
}
return arr;
}
//checking if array has any duplicate values
var hasDuplicates = (array) => {
var valuesSoFar = [];
for (let i=0; i < array.length; i++) {
var item = array[i];
if (valuesSoFar.includes(item)) {
return true
} else {
valuesSoFar.push(item)
};
}
return false;
};
//checking the difference between to arrays
Array.prototype.diff = function(a) {
return this.filter(function (i) {return a.indexOf(i) < 0;})
}
//returns the possible columns taking the asigned numbers into account
var possibleColumns = (grid) => {
let numArr = []
let columns = []
let revertedGrid = []
//creating an array with sub arrays
for (let i=0; i < n; i++){
revertedGrid.push([])
}
//creating a array of numbers to take the difference from
for (let i=1; i < n+1;i++){
numArr.push(i)
}
//reverting the grid
for (let i=0;i < n; i++) {
for (let j=0; j < grid.length;j++){
revertedGrid[i].push(grid[j][i])
}
}
//taking the possible columns
for (let i = 0; i < n; i++) {
var possibleNumbers = numArr.diff(revertedGrid[i])
columns.push(possibleNumbers)
}
return columns
}
//takeing the possible Columns and calculating each row
function columnToRow(col) {
let row = []
let i = 0
while(row.length < n){
var num = (col[i].diff(row))[0]
// TODO: find better solution for this workaround
//workaround to shuffle the current column by place num at the end
if (randomNumber(2) === 2) col[i].push(col[i].splice(col[i].indexOf(num),1)[0])
// check if it ran into dead-end
if(num === undefined) {
//going back to the beginning
i = 0
row = []
} else {
row.push(num)
i++
}}
return row
}
function makeGrid() {
let grid = []
grid.push(ran(n))
while (grid.length < n){
grid.push(columnToRow(possibleColumns(grid)))
}
return grid
}
var grid = makeGrid()
let table = document.getElementById('myTabel')
let rowIndex = 0
let cellIndex = 0
let i = 0
function addTabel(n) {
for (let i = 0; i < n; i++){
let newRow = table.insertRow(i);
for (let j = 0; j < n; j++) {
let newCell = newRow.insertCell(j);
}
}
}
function typeWriter(){
if (cellIndex >= n) {
cellIndex = 0
rowIndex++
}
if (i < n * n) {
let number = document.createTextNode(JSON.stringify(grid[rowIndex][cellIndex]));
let cell = table.rows[rowIndex].cells[cellIndex]
cell.appendChild(number)
}
i++
cellIndex++
setTimeout(typeWriter, speed);
}
addTabel(n)
typeWriter()
// console.log(grid);
// function addTabel(n) {
// let table = document.getElementById('myTabel')
//
// for (let i=0; i < n; i++){
// let newRow = table.insertRow(i);
// //making cells and filling them with the grid's numbers
// for (let j = 0; j < n; j++) {
// let newCell = newRow.insertCell(j);
// let number = document.createTextNode(JSON.stringify(grid[i][j]));
// newCell.appendChild(number);
// console.log('added');
// }
// }
// }
//
// function typeWriter(){
// if (i < text.length ) {
// type += text[i]
// ele.innerHTML = type
// }
// else {
// type = type.slice(0,type.length-1)
// ele.innerHTML = type
// }
// i++;
// //when first loop is finished
// if (i >= (text.length *2)) {
// j++
// text = grid[j]
// i = 0
// }
// setTimeout(typeWriter, speed);
// }