-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
65 lines (53 loc) · 1.53 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
//Variable initializations
var keys = ['key1', 'key2', 'key3'];
var values = ['value1', 'value2', 'value3']
var size;
var csvString;
var currentState;
var rows;
var counter = 0;
var showCounter = true;//get it from config.js
const request = async()=>{
const response = await fetch('data.csv');
csvString = await response.text();
console.log(csvString);
setRows(csvString);
//If current state is null, then we are showing the answer
currentRow = rows[getRandomInt()];
showQuestion(currentRow);
}
request();
function setRows(csvString) {
rows = csvString.split(/\r\n|\n|\r/);
size = rows.length;
console.log(rows)
}
function getRandomInt() {
return Math.floor(Math.random() * size);
}
function showQuestion(row) {
document.getElementById("flashcard_div").innerHTML = row.split(",")[0];
if(showCounter) {
document.getElementById("counter").innerHTML = ++counter;
}
}
function showAnswer(row) {
document.getElementById("flashcard_div").innerHTML = (row.split(",")).slice(1).join(",");
}
function processInput() {
if (currentRow !== null) {
showAnswer(currentRow);
currentRow = null;
} else {
currentRow = rows[getRandomInt()];
showQuestion(currentRow);
}
}
document.addEventListener('keypress', function(e) {
if (e.code === 'Enter' || e.code === 'Space') {
processInput();
}
});
document.addEventListener('click', function(event) {
processInput();
}, false);