-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFedericoPalma.html
72 lines (62 loc) · 2.25 KB
/
FedericoPalma.html
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
<!DOCTYPE html>
<html>
<head>
<title>Prova Intermedia</title>
<style>
.mt-2 {
margin-top: 1em;
}
</style>
</head>
<body>
<h1>Prova Intermedia</h1>
<input type="text" id="inputText">
<button onclick="checkThreshold()">Conferma</button>
<br>
<button onclick="resetThreshold()" class="mt-2">Reset</button>
<p id="result"></p>
<script>
//Creo una variabile threshold per la lunghezza da controllare
var threshold = 10;
//prendo gli elementi dal localstorage se presenti
var storageValue = localStorage.getItem("count");
//inizializzo l'oggetto count con i valori di default
var count = {
aboveThreshold: storageValue != undefined ? JSON.parse(storageValue).aboveThreshold : 0,
belowThreshold: storageValue != undefined ? JSON.parse(storageValue).belowThreshold : 0
};
//stampo i valori nel paragrafo result
printResult();
//funzione per resettare i valori
function resetThreshold() {
count.aboveThreshold = 0;
count.belowThreshold = 0;
localStorage.removeItem("count");
printResult();
}
//funzione per controllare la lunghezza della stringa
function checkThreshold() {
var inputText = document.getElementById("inputText").value;
inputText = inputText.trim();
if (inputText.length > threshold) {
count.aboveThreshold++;
} else {
count.belowThreshold++;
}
localStorage.setItem("count", JSON.stringify(count));
printResult();
}
//funzione per stampare i valori nel paragrafo result
function printResult() {
var result = document.getElementById("result");
result.innerHTML = "Sopra la soglia: " + count.aboveThreshold + "<br>Sotto la soglia: " + count.belowThreshold;
}
//funzione per controllare se è stato premuto il tasto invio
document.getElementById("inputText").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
checkThreshold();
}
});
</script>
</body>
</html>