-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (46 loc) · 1.59 KB
/
index.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
const testo = document.getElementById("poesia");
let submit = document.getElementById("submit");
let reset = document.getElementById("reset");
let baciate = document.getElementById("baciate");
let alternate = document.getElementById("alternate");
let incrociate = document.getElementById("incrociate");
let RiBa = 0; //rime baciate
let RiAl = 0; //rime alternate
let RiIn = 0; //rime incrociate
reset.addEventListener("click", (e) => {
testo.value = "";
});
submit.addEventListener("click", (e) => {
RiBa = 0;
RiAl = 0;
RiIn = 0;
trovarime(testo.value);
// aggiorna i valori della tabella
baciate.innerText = RiBa;
alternate.innerText = RiAl;
incrociate.innerText = RiIn;
});
const trovarime = (text) => {
// divide il testo in un array di stringhe
const versi = text.split("\n");
// crea un array formato dalle finali dei versi
let finali = [];
versi.forEach(element => {
if(element !== "")
finali.push(getFinale(element));
});
// conta quante e quali rime ci sono (relative ad ogni verso)
for(let i = 0; i<finali.length;i++){
if(finali[i] === finali[i+1]) RiBa++;
if(finali[i] === finali[i+2] && finali[i+2]) RiAl++;
if(finali[i] === finali[i+3] && finali[i+1] === finali[i+2] && finali[i+1]) RiIn++;
}
}
const getFinale = (str) => {
// estrapola le ultime 3 lettere di ogni verso
let end = str.split(" ")[-1] || str;
const regex = /[”!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/g;
end = end.replace(regex, '');
console.log(end);
return end.slice(-3);
}