-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
85 lines (77 loc) · 2.66 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
var preguntas;
var pregunta;
var respuestas;
var buffer = "";
var result = false;
function loadWord(){
if(preguntas === undefined || preguntas.length < 3){
// Asynchronously get the word that will be displayed on the next call
$.ajax({
url: '/api/getWord/?num_words=20',
type: 'GET',
dataType: 'json',
success: function(json){
preguntas = json;
}
});
}
// Display the word that was previously buffered
pregunta = preguntas[0]["word"];
respuestas = preguntas[0]["translation"];
preguntas.shift()
if(pregunta.length > 0){
$(".upper-span").text(pregunta);
var el = $(".upper-text"),
newone = el.clone(true);
el.addClass("toRemove");
el.before(newone);
$(".toRemove").remove();
}
};
function sendResult(word, result){
$.ajax({
url: '/api/postResult/?word=' + word + '&result=' + result,
type: 'GET',
dataType: 'json'
});
};
function captureBackspace(event){
if(event.which == 8){
buffer = buffer.substring(0, buffer.length - 1);
$(".lower-span").text(buffer);
}
};
function captureKeyboard(event){
if(event.which == 13){
$(".lower-span").removeClass("init");
if(preguntas !== undefined && respuestas !== undefined){ // This is unnecessary if no word has been loaded yet, we are still displaying the first screen
result = false;
// Check the buffer against all available translations for a match
respuestas.forEach(function (item, index){
let norm = item.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase(); // Delete accents and change to lowercase
if (buffer.toLowerCase() === norm){
result = true;
}
});
sendResult(pregunta, result);
if(result){
$(".info-text span").text("Correct ! Réponses possibles : " + respuestas.toString());
$(".info-text span").removeClass("incorrect");
} else {
$(".info-text span").text("Incorrect ! Réponses possibles : " + respuestas.toString());
$(".info-text span").addClass("incorrect");
}
}
buffer = "";
loadWord();
}
var c = String.fromCharCode(event.which);
var b = c.replace(/[^a-z0-9-! ]/gi,''); // Only allowing a simple character set
buffer = buffer + b;
$(".lower-span").text(buffer);
};
$(document).ready(function(){
$(document).keypress(captureKeyboard);
$(document).keydown(captureBackspace);
loadWord();
});