diff --git a/RETI/bianchi_emanuele/DHCP/bianchi_apipa.pkt b/RETI/bianchi_emanuele/DHCP/bianchi_apipa.pkt new file mode 100644 index 00000000..67094c28 Binary files /dev/null and b/RETI/bianchi_emanuele/DHCP/bianchi_apipa.pkt differ diff --git a/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_01.pkt b/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_01.pkt new file mode 100644 index 00000000..707945f7 Binary files /dev/null and b/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_01.pkt differ diff --git a/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_03.pkt b/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_03.pkt new file mode 100644 index 00000000..08384613 Binary files /dev/null and b/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_03.pkt differ diff --git a/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_04.pkt b/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_04.pkt new file mode 100644 index 00000000..1691c72f Binary files /dev/null and b/RETI/bianchi_emanuele/DHCP/bianchi_dhcp_04.pkt differ diff --git a/RETI/bianchi_emanuele/RIP/bianchi_RIP_02_start.pkt b/RETI/bianchi_emanuele/RIP/bianchi_RIP_02_start.pkt new file mode 100644 index 00000000..b011d708 Binary files /dev/null and b/RETI/bianchi_emanuele/RIP/bianchi_RIP_02_start.pkt differ diff --git a/RETI/bianchi_emanuele/VLSM/VLSM_03.pkt b/RETI/bianchi_emanuele/VLSM/VLSM_03.pkt new file mode 100644 index 00000000..a1946f14 Binary files /dev/null and b/RETI/bianchi_emanuele/VLSM/VLSM_03.pkt differ diff --git a/RETI/bianchi_emanuele/VLSM/bianchi_compito_10.pkt b/RETI/bianchi_emanuele/VLSM/bianchi_compito_10.pkt new file mode 100644 index 00000000..83185f20 Binary files /dev/null and b/RETI/bianchi_emanuele/VLSM/bianchi_compito_10.pkt differ diff --git a/RETI/bianchi_emanuele/routing/bianchi_routing_es_01.pkt b/RETI/bianchi_emanuele/routing/bianchi_routing_es_01.pkt new file mode 100644 index 00000000..ad320623 Binary files /dev/null and b/RETI/bianchi_emanuele/routing/bianchi_routing_es_01.pkt differ diff --git a/RETI/bianchi_emanuele/routing/bianchi_routing_es_02-3.pkt b/RETI/bianchi_emanuele/routing/bianchi_routing_es_02-3.pkt new file mode 100644 index 00000000..d5e8a5ad Binary files /dev/null and b/RETI/bianchi_emanuele/routing/bianchi_routing_es_02-3.pkt differ diff --git a/RETI/bianchi_emanuele/routing/bianchi_routing_es_04.pkt b/RETI/bianchi_emanuele/routing/bianchi_routing_es_04.pkt new file mode 100644 index 00000000..8e8964f8 Binary files /dev/null and b/RETI/bianchi_emanuele/routing/bianchi_routing_es_04.pkt differ diff --git a/RETI/bianchi_emanuele/routing/bianchi_routing_es_05.pkt b/RETI/bianchi_emanuele/routing/bianchi_routing_es_05.pkt new file mode 100644 index 00000000..0b8fc771 Binary files /dev/null and b/RETI/bianchi_emanuele/routing/bianchi_routing_es_05.pkt differ diff --git a/RETI/bianchi_emanuele/routing/bianchi_routing_es_06.pkt b/RETI/bianchi_emanuele/routing/bianchi_routing_es_06.pkt new file mode 100644 index 00000000..4fbb68ca Binary files /dev/null and b/RETI/bianchi_emanuele/routing/bianchi_routing_es_06.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_01.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_01.pkt new file mode 100644 index 00000000..7cf3fa38 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_01.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_02.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_02.pkt new file mode 100644 index 00000000..89887e43 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_02.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_03.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_03.pkt new file mode 100644 index 00000000..2d1706e0 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_03.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_04.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_04.pkt new file mode 100644 index 00000000..566e4053 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_04.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_05.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_05.pkt new file mode 100644 index 00000000..3c8dfa1d Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_05.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_06.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_06.pkt new file mode 100644 index 00000000..fb2dcc61 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_06.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_07.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_07.pkt new file mode 100644 index 00000000..2b403de9 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_07.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_08.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_08.pkt new file mode 100644 index 00000000..a6e34e02 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_08.pkt differ diff --git a/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_09.pkt b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_09.pkt new file mode 100644 index 00000000..7edb2661 Binary files /dev/null and b/RETI/bianchi_emanuele/vlan/bianchi_vlan_es_09.pkt differ diff --git a/RETI/routing/bianchi_routing_es_01.pkt b/RETI/routing/bianchi_routing_es_01.pkt new file mode 100644 index 00000000..ad320623 Binary files /dev/null and b/RETI/routing/bianchi_routing_es_01.pkt differ diff --git a/RETI/routing/bianchi_routing_es_02-3.pkt b/RETI/routing/bianchi_routing_es_02-3.pkt new file mode 100644 index 00000000..d5e8a5ad Binary files /dev/null and b/RETI/routing/bianchi_routing_es_02-3.pkt differ diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_09.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_09.html new file mode 100644 index 00000000..a15e8977 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_09.html @@ -0,0 +1,12 @@ + + + + + + esercizio 09 + + + + + + \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_09.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_09.js new file mode 100644 index 00000000..a8d5dad4 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_09.js @@ -0,0 +1,30 @@ +console.log("prova") + +let pwd +let nome = prompt("USERNAME:") +nome == null && alert("Cancel") +nome != null && (nome == "Admin" || alert("I don't know")); +nome == "Admin" && (pwd = prompt("PASSWORD")) +nome == "Admin" && pwd == null && alert("Cancel") +nome == "Admin" && pwd != null && pwd == "Agnell1no" || alert("Wrong Password") +nome == "Admin" && pwd == "Agnell1no" && alert("Welcome") + +/* +let nome = prompt("USERNAME:") +if(nome != "Admin") +{ + alert("i don't know") +} +else +{ + let password = prompt("inserisci la password"); + if(password != "Agnell1no") + { + alert("wrong password"); + } + else + { + alert("welcome"); + } +} +*/ \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_10.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_10.js new file mode 100644 index 00000000..1765433c --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_10.js @@ -0,0 +1,11 @@ +//Realizzare il ciclo for...in su un oggetto utente NON utilizzando il for...in ma un ciclo for normale +let utente = { + nome: "Mario", + cognome: "Rossi", + età: 30 +} + +for (let i = 0; i < Object.keys(utente).length; i++) +{ + console.log(`la chiave ${i} ha valore ${utente[i]}`); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_11.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_11.js new file mode 100644 index 00000000..f7df0476 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_11.js @@ -0,0 +1,43 @@ +// Funzione costruttore per Automobile +function creaAutomobile(marca, modello, anno, colore, km) { + return { + marca: marca, + modello: modello, + anno: anno, + colore: colore, + km: km, + + // Metodo 1: Avvia motore + avviaMotore: function() { + console.log(`${this.marca} ${this.modello} avviato!`); + }, + + // Metodo 2: Mostra info + mostraInfo: function() { + console.log(`Auto: ${this.marca} ${this.modello} (${this.anno})`); + console.log(`Colore: ${this.colore}, KM: ${this.km}`); + }, + + // Metodo 3: Aggiungi km + guida: function(kmAggiuntivi) { + this.km += kmAggiuntivi; + console.log(`Guidato ${kmAggiuntivi}km. Totale: ${this.km}km`); + } + }; +} + +// Main +const auto1 = creaAutomobile("Fiat", "Panda", 2020, "Bianca", 15000); +const auto2 = creaAutomobile("Volkswagen", "Golf", 2022, "Nera", 8000); +const auto3 = creaAutomobile("Toyota", "Yaris", 2019, "Rossa", 25000); +const auto4 = creaAutomobile("Renault", "Clio", 2021, "Blu", 12000); +const auto5 = creaAutomobile("Ford", "Fiesta", 2023, "Grigia", 3000); + +console.log("=== ESERCIZIO JS_11 ===\n"); + +[auto1, auto2, auto3, auto4, auto5].forEach((auto, index) => { + console.log(`\n--- Auto ${index + 1} ---`); + auto.avviaMotore(); + auto.mostraInfo(); + auto.guida(100); +}); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_12.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_12.js new file mode 100644 index 00000000..fd9c03a7 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_12.js @@ -0,0 +1,53 @@ +function creaAnimale(nome, specie, eta) { + return { + nome, specie, eta, + mangia: () => console.log(`${nome} sta mangiando`), + dorme: () => console.log(`${nome} sta dormendo`), + enumera: function() { + console.log("Attributi Animale:", Object.keys(this)); + } + }; +} + +function creaAutomobile(marca, modello, km) { + return { + marca, modello, km, + avvia: () => console.log(`${marca} ${modello} avviata`), + frena: () => console.log(`${marca} ${modello} frena`), + enumera: function() { + console.log("Attributi Automobile:", Object.keys(this)); + } + }; +} + +function creaPoligono(lati, colore, area) { + return { + lati, colore, area, + ruota: () => console.log(`Poligono ${lati} lati ruota`), + scala: () => console.log("Poligono scalato"), + enumera: function() { + console.log("Attributi Poligono:", Object.keys(this)); + } + }; +} + +// Main +console.log("=== ESERCIZIO JS_12 ===\n"); + +console.log("ANIMALI:"); +const cane1 = creaAnimale("Rex", "Cane", 3); +const cane2 = creaAnimale("Fido", "Cane", 5); +cane1.enumera(); +cane2.enumera(); + +console.log("\nAUTOMOBILI:"); +const auto1 = creaAutomobile("Fiat", "Panda", 15000); +const auto2 = creaAutomobile("VW", "Golf", 8000); +auto1.enumera(); +auto2.enumera(); + +console.log("\nPOLIGONI:"); +const triangolo1 = creaPoligono(3, "rosso", 10); +const triangolo2 = creaPoligono(3, "blu", 15); +triangolo1.enumera(); +triangolo2.enumera(); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_13.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_13.js new file mode 100644 index 00000000..8b96aab8 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_13.js @@ -0,0 +1,16 @@ +function sommaFinoN(n) { + n = Number(n); + if (!Number.isInteger(n) || n < 0) return 0; + + let somma = 0; + for (let i = 0; i <= n; i++) { + somma += i; + } + return somma; +} + +// Main +console.log("=== ESERCIZIO JS_13 ===\n"); +console.log("sommaFinoN(5):", sommaFinoN(5)); // 15 +console.log("sommaFinoN('10'):", sommaFinoN("10")); // 55 +console.log("sommaFinoN(8):", sommaFinoN(8)); // 36 \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_14.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_14.js new file mode 100644 index 00000000..86ed2c02 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_14.js @@ -0,0 +1,24 @@ +function differenzaOrari(ora1, ora2) { + const tempo1 = (ora1.h || 0) * 3600000 + (ora1.m || 0) * 60000 + (ora1.s || 0) * 1000; + const tempo2 = (ora2.h || 0) * 3600000 + (ora2.m || 0) * 60000 + (ora2.s || 0) * 1000; + + const diffMs = Math.abs(tempo2 - tempo1); + + return { + ms: diffMs, + secondi: Math.floor(diffMs / 1000), + minuti: Math.floor(diffMs / 60000), + ore: Math.floor(diffMs / 3600000) + }; +} + +// Main +console.log("=== ESERCIZIO JS_14 ===\n"); + +const orario1 = { h: 10, m: 30, s: 45 }; +const orario2 = { h: 12, m: 15, s: 20 }; +console.log("Differenza 10:30:45 - 12:15:20:", differenzaOrari(orario1, orario2)); + +const orario3 = { h: 8, m: 0, s: 0 }; +const orario4 = { h: 9, m: 30, s: 0 }; +console.log("Differenza 08:00:00 - 09:30:00:", differenzaOrari(orario3, orario4)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_15.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_15.js new file mode 100644 index 00000000..2eff230c --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_15.js @@ -0,0 +1,22 @@ +function fibonacci(n) { + n = Number(n); + if (!Number.isInteger(n) || n < 0) return 0; + + if (n === 0 || n === 1) return n; + + let a = 0, b = 1; + for (let i = 2; i <= n; i++) { + let temp = a + b; + a = b; + b = temp; + } + return b; +} + +// Main +console.log("=== ESERCIZIO JS_15 ===\n"); +console.log("fibonacci(0):", fibonacci(0)); // 0 +console.log("fibonacci(5):", fibonacci(5)); // 5 +console.log("fibonacci(8):", fibonacci(8)); // 21 +console.log("fibonacci(10):", fibonacci(10)); // 55 +console.log("fibonacci(12):", fibonacci(12)); // 144 diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_16.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_16.js new file mode 100644 index 00000000..93c1e565 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_16.js @@ -0,0 +1,23 @@ +function orarioMinore(ora1, ora2, ora3) { + const tempo1 = (ora1.h || 0) * 3600 + (ora1.m || 0) * 60 + (ora1.s || 0); + const tempo2 = (ora2.h || 0) * 3600 + (ora2.m || 0) * 60 + (ora2.s || 0); + const tempo3 = (ora3.h || 0) * 3600 + (ora3.m || 0) * 60 + (ora3.s || 0); + + const tempi = [tempo1, tempo2, tempo3]; + const minTempo = Math.min(...tempi); + + if (tempo1 === minTempo) return ora1; + if (tempo2 === minTempo) return ora2; + return ora3; +} + +// Main +console.log("=== ESERCIZIO JS_16 ===\n"); + +console.log("Minore di 10:30, 09:15, 11:00:", orarioMinore( + {h:10,m:30}, {h:9,m:15}, {h:11,m:0} +)); + +console.log("Minore di 14:20:30, 14:10:45, 14:25:10:", orarioMinore( + {h:14,m:20,s:30}, {h:14,m:10,s:45}, {h:14,m:25,s:10} +)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_17.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_17.js new file mode 100644 index 00000000..1ae9f7a8 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_17.js @@ -0,0 +1,9 @@ +function max2(a, b) { + return a > b ? a : b; +} + +// Main +console.log("=== ESERCIZIO JS_17 ===\n"); +console.log("max2(5, 8):", max2(5, 8)); +console.log("max2(12, 7):", max2(12, 7)); +console.log("max2(-1, 0):", max2(-1, 0)); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_18.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_18.js new file mode 100644 index 00000000..b8746d41 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_18.js @@ -0,0 +1,9 @@ +function max3(a, b, c) { + return a > b ? (a > c ? a : c) : (b > c ? b : c); +} + +// Main +console.log("=== ESERCIZIO JS_18 ===\n"); +console.log("max3(5, 8, 3):", max3(5, 8, 3)); +console.log("max3(12, 15, 7):", max3(12, 15, 7)); +console.log("max3(10, 10, 10):", max3(10, 10, 10)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_19.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_19.js new file mode 100644 index 00000000..960b77c0 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_19.js @@ -0,0 +1,35 @@ +function analizzaOggetto(obj) { + console.log("=== ANALISI OGGETTO ===\n"); + for (let [nome, valore] of Object.entries(obj)) { + const tipo = typeof valore; + const valoreVisualizzato = tipo === 'string' ? valore.toLowerCase() : valore; + console.log(`Nome: ${nome}`); + console.log(`Tipo: ${tipo}`); + console.log(`Valore: ${valoreVisualizzato}\n`); + } +} + +// Main +console.log("=== ESERCIZIO JS_19 ===\n"); + +const persona1 = { + nome: "Mario ROSSI", + eta: 30, + citta: "ROMA", + lavora: true, + hobby: ["calcio", "tennis"], + salario: 2500.50 +}; + +const auto1 = { + marca: "FIAT", + modello: "PANDA", + anno: 2020, + colore: "NERO", + km: 15000, + nuovo: false +}; + +analizzaOggetto(persona1); +console.log("\n--- Secondo oggetto ---\n"); +analizzaOggetto(auto1); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_20.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_20.js new file mode 100644 index 00000000..5b61d4ba --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_20.js @@ -0,0 +1,111 @@ +function Automobile(persone = 0, velocita = 0, rapporto = 1, accesa = false) { + return { + persone, + velocita, + rapporto, + accesa, + + setPersone: function(nuovePersone) { + if (this.velocita > 0) { + console.log("Impossibile: auto in movimento"); + return false; + } + if (nuovePersone >= 0 && nuovePersone <= 5) { + this.persone = nuovePersone; + this.mostraStato(); + return true; + } + return false; + }, + + setVelocita: function(nuovaVelocita) { + if (!this.accesa) { + console.log("Auto spenta"); + return false; + } + if (nuovaVelocita < 0 || nuovaVelocita > 180) return false; + if (Math.abs(nuovaVelocita - this.velocita) > 30) { + console.log("Variazione troppo rapida (±30km/h max)"); + return false; + } + this.velocita = nuovaVelocita; + this.mostraStato(); + return true; + }, + + aumentaRapporto: function() { + if (!this.accesa) { + console.log("Auto spenta"); + return false; + } + if (this.rapporto < 6) { + this.rapporto++; + this.mostraStato(); + return true; + } + console.log("Rapporto massimo raggiunto"); + return false; + }, + + diminuisciRapporto: function() { + if (!this.accesa) { + console.log("Auto spenta"); + return false; + } + if (this.rapporto > 1) { + this.rapporto--; + this.mostraStato(); + return true; + } + console.log("Rapporto minimo raggiunto"); + return false; + }, + + toggleAccensione: function() { + this.accesa = !this.accesa; + console.log(this.accesa ? "Auto ACCESA" : "Auto SPENTA"); + this.mostraStato(); + return this.accesa; + }, + + mostraStato: function() { + console.log(`👥 ${this.persone} | ⚡ ${this.velocita}km/h | ⚙️ R${this.rapporto} | ${this.accesa}`); + } + }; +} + +// Main - Test completo +console.log("=== ESERCIZIO JS_20 ===\n"); + +const auto = new Automobile(); + +console.log("1. Accendo auto:"); +auto.toggleAccensione(); + +console.log("\n2. Imposto persone (auto accesa):"); +auto.setPersone(2); + +console.log("\n3. Spengo auto e cambio persone:"); +auto.toggleAccensione(); +auto.setPersone(4); +auto.toggleAccensione(); + +console.log("\n4. Test velocità (variazioni ±30):"); +auto.setVelocita(30); +auto.setVelocita(50); +auto.setVelocita(25); // OK +// auto.setVelocita(80); // Fallisce + +console.log("\n5. Test rapporti:"); +auto.aumentaRapporto(); +auto.aumentaRapporto(); +auto.diminuisciRapporto(); +auto.aumentaRapporto(); +auto.aumentaRapporto(); // Fallisce + +console.log("\n6. Cambio persone con velocità > 0:"); +auto.setPersone(1); // Fallisce + +console.log("\n7. Spengo tutto:"); +auto.toggleAccensione(); +auto.mostraStato(); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_21.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_21.js new file mode 100644 index 00000000..842ed8a2 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_21.js @@ -0,0 +1,14 @@ +function opera(a, b, callback) { + return callback(a, b); +} + +// Callback +const somma = (a, b) => a + b; +const prodotto = (a, b) => a * b; +const potenza = (a, b) => a ** b; + +// Main +console.log("=== ESERCIZIO JS_21 ===\n"); +console.log("Somma 5+3:", opera(5, 3, somma)); +console.log("Prodotto 4*6:", opera(4, 6, prodotto)); +console.log("Potenza 2^4:", opera(2, 4, potenza)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_22.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_22.js new file mode 100644 index 00000000..22cebda4 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_22.js @@ -0,0 +1,12 @@ +const somma = (a, b) => a + b; +const sottrazione = (a, b) => a - b; +const moltiplicazione = (a, b) => a * b; +const divisione = (a, b) => b !== 0 ? a / b : "Errore: divisione per 0"; + +// Main +console.log("=== ESERCIZIO JS_22 ===\n"); + +console.log("SOMMA:", somma(5, 3), somma(10, 2)); +console.log("SOTTRAZIONE:", sottrazione(10, 4), sottrazione(7, 2)); +console.log("MOLTIPLICAZIONE:", moltiplicazione(6, 4), moltiplicazione(3, 7)); +console.log("DIVISIONE:", divisione(20, 5), divisione(10, 2)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_65.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_65.js new file mode 100644 index 00000000..7da7402a --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_65.js @@ -0,0 +1,10 @@ +let utente = { + nome: "Jon", + role: "user" +}; + +const {nome, role, admin: admin = false} = utente; + +console.log(nome); +console.log(role); +console.log(admin); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_66.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_66.js new file mode 100644 index 00000000..5a98dba0 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_66.js @@ -0,0 +1,3 @@ +let item = ["egg", 0.25, 12]; +const [name, price, quantity] = item; +console.log('item: ${name}, Quantity: ${quantity}, Price: ${price}'); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_67.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_67.js new file mode 100644 index 00000000..006ad1d0 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_67.js @@ -0,0 +1,3 @@ +let numbers = [3, 5, 4, 2, 6, 1]; +let [one, two, three, four, five, six] = [...numbers].sort; +console.log('One: ${one}, Two: ${two}, Trhee: ${three}, Four: ${four}, Five: ${five}, Six: ${six}'); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_68.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_68.js new file mode 100644 index 00000000..cd8bebe4 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_68.js @@ -0,0 +1,2 @@ +const students = ['Cristina', 'Jon', 'Alexander'] +const [, , lastname] \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_70.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_70.js new file mode 100644 index 00000000..a7a8942a --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_70.js @@ -0,0 +1,71 @@ +/* OOP + scrivere un programma in JS che: + 1. implementi una classe Calcolatrice che fornisce le 4 operazioni aritmetiche base + 2. in aggiunta questa classe permette l'esecuzione di espressioni del tipo: + 5+3-2+4*5-2/2 (non ci sono precedenze) + 3. scrivere il main che utilizzando la classe Calcolatrice, stampi sulla console le seguenti espressioni ed il + risultato di ciascuna: + 6-2^ * 5 + 8/4 = + 11^ * 3 + 5 - 5/11 = + 29 + 2 + (- 7/3) / 2 +1^ * 2=? + 4. Consegnare su github il file con nome /cognome|_esercizio_js_70.js*/ + +// cognome_esercizio_js_70.js + +class Calcolatrice { + somma(a, b) { + return a + b; + } + + sottrai(a, b) { + return a - b; + } + + moltiplica(a, b) { + return a * b; + } + + dividi(a, b) { + return a / b; + } + + // calcolo SENZA precedenze (da sinistra a destra) + calcola(expr) { + // tolgo spazi + expr = expr.replaceAll(" ", ""); + let risultato = ""; + let numero = ""; + let operatore = "+"; + for (let i = 0; i < expr.length; i++) { + let char = expr[i]; + // se è numero lo aggiungo + if (!isNaN(char)) { + numero += char; + } else { + // faccio il calcolo + risultato = this.esegui(risultato, numero, operatore); + operatore = char; + numero = ""; + } + } + // ultimo numero + risultato = this.esegui(risultato, numero, operatore); + return risultato; + } + + esegui(a, b, op) { + a = Number(a); + b = Number(b); + if (op === "+") return this.somma(a, b); + if (op === "-") return this.sottrai(a, b); + if (op === "*") return this.moltiplica(a, b); + if (op === "/") return this.dividi(a, b); + } +} + + +let calc = new Calcolatrice(); + +console.log("6-2*5+8/4 =", calc.calcola("6-2*5+8/4")); +console.log("11*3+5-5/11 =", calc.calcola("11*3+5-5/11")); +console.log("29+2-7/3/2+1*2 =", calc.calcola("29+2-7/3/2+1*2")); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_71.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_71.js new file mode 100644 index 00000000..87f2e3bf --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_71.js @@ -0,0 +1,43 @@ +// classe Poligono +class Poligono { + + constructor(lati, lunghezza) { + this.lati = lati; + this.lunghezza = lunghezza; + } + + // perimetro + perimetro() { + return this.lati * this.lunghezza; + } + + // area (formula poligono regolare) + area() { + return (this.lati * this.lunghezza * this.lunghezza) / (4 * Math.tan(Math.PI / this.lati)); + } + + stampa() { + console.log("Lati:", this.lati, "- Lunghezza lato:", this.lunghezza); + console.log("Perimetro:", this.perimetro().toFixed(2)); + console.log("Area:", this.area().toFixed(2)); + console.log("------------------------"); + } +} + + +// ===== MAIN ===== + +let triangolo = new Poligono(3, 15); +let quadrato = new Poligono(4, 10); +let pentagono = new Poligono(5, 21); +let esagono = new Poligono(6, 18); +let ettagono = new Poligono(7, 9); +let ottagono = new Poligono(8, 12); + +// stampa risultati +triangolo.stampa(); +quadrato.stampa(); +pentagono.stampa(); +esagono.stampa(); +ettagono.stampa(); +ottagono.stampa(); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_72.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_72.js new file mode 100644 index 00000000..bcc1e4a2 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_72.js @@ -0,0 +1,51 @@ +/* Esercizio 72 +scrivere un programma in JS che: +1. implementi una funzione confronta OBJ che confronta due oggetti e restituisce true se sono uguali e false in +caso contrario +2. scrivere un main che definisce i due seguenti oggetti +let studente = new Studente("Mario", 4); +let docente = new Docente("Mario", 4); +e li confronta utilizzando la funzione confronta OBJ*/ + +// cognome_esercizio_js_72.js + +// classe Studente +class Studente { + constructor(nome, classe) { + this.nome = nome; + this.classe = classe; + } +} + +// classe Docente +class Docente { + constructor(nome, classe) { + this.nome = nome; + this.voto = classe; + } +} + +function confrontaOBJ(obj1, obj2) { + + let chiavi1 = Object.keys(obj1); + let chiavi2 = Object.keys(obj2); + + if (chiavi1.length !== chiavi2.length) { + return false; + } + + for (let key of chiavi1) { + if (obj1[key] !== obj2[key]) { + return false; + } + } + + return true; +} + +let studente = new Studente("Mario", 4); +let docente = new Docente("Mario", 4); + +let risultato = confrontaOBJ(studente, docente); + +console.log("Sono uguali?", risultato); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_73.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_73.html new file mode 100644 index 00000000..8c286a00 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_73.html @@ -0,0 +1,26 @@ + + + + + Esercizio 73_js + + + +

Registrazione Utente

+ +
+

+

+

+

+ + +
+ +

Output:

+
+ + + + + \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_73.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_73.js new file mode 100644 index 00000000..5b0d712d --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_73.js @@ -0,0 +1,93 @@ +/* Esercizio js_73 +JS Objects +Per svolgere l'esercizio è necessario utilizzare un form +Vediamo qui il classico modo di processare il form client-side e non server-side +
+ +
+collego un event handler chiamato process_form all'evento click sul pulsante submit del form Ora inibisco il normale funzionamento del form per gestirlo client-side e non server-side +Aggiungo un event handler all'inizio del file .js +// aggiunge un handler all'evento DOMContentLoaded +// che viene generato quando la pagina HTML ha terminato il suo caricamento +document.addEventListener("DOMContentLoaded", function() { +// ricata l'oggetto DOM del form +let form_utente = document.getElementById(); +//aggiunge un handler all'evento submit del form +//in modo da inibire il normale funzionamento (invio del form verso il server) +form_utente.addEventListener("submit", function (event) { +event.preventDefault(); +}); +}); +Infine definisco l'event_handler per il processamento del form +//processa il form quando l'utente preme submit +function process_form() { +} +console.log("process form"); +form = document.forms[< form><]; +console.log(form.elements[].value); +In questo modo quando l'utente preme il pulsante submit del form, il form stesso non sarà inviato al server event.preventDefault() +Nell'handler di processamento del form posso accedere ai vari campi del form +form.elements[].value +Questo è il classico modo di processare un form lato client-side grazie a JS +1. Creare una pagina HTML e JS contenente un form di registrazione utente +2. Il form deve contenere i seguenti campi +1. nome +2. cognome +3. età +4. colore dei capelli +3. Alla pressione del pulsante submit, il codice deve: +Creare un oggetto vuoto di nome User con i relativi attributi valorizzati con i valori inseriti nel form +Definire un metodo dell'oggetto chiamato descrivi +Scandire dinamicamente tutte le proprietà dell'oggetto ed aggiungere dinamicamente alla pagina HTML +un tag p con il valore della proprietà letta +Invocare il metodo descrivi che deve stampare su console il segunete messaggio: "ciao io sono l'utente di anni e con i capelli color ", dove i valori tra <> sono sostituiti con le proprietà dell'oggetto +Rimuovere le proprietà età e colore dei capelli dall'oggetto e con un ciclo aggiungere dinamicamente alla pagina HTML un tag p con il valore della proprietà rimanenti +4. Consegnare su github il file con nome /cognome/_esercizio_js_73.html e /cognome/_esercizio_js_73.js */ + + +document.addEventListener("DOMContentLoaded", function () { + let form = document.getElementById("formUtente"); + form.addEventListener("submit", function (event) { + event.preventDefault(); + }); +}); + + +function process_form() { + let form = document.forms["formUtente"]; + let nome = form.elements["nome"].value; + let cognome = form.elements["cognome"].value; + let eta = form.elements["eta"].value; + let capelli = form.elements["capelli"].value; + let User = { + nome: nome, + cognome: cognome, + eta: eta, + capelli: capelli, + descrivi: function () { + console.log("ciao io sono l'utente " + this.nome + " " + this.cognome + + " di anni " + this.eta + " e con i capelli color " + this.capelli); + } + }; + let output = document.getElementById("output"); + output.innerHTML = ""; + for (let key in User) { + if (typeof User[key] !== "function") { + let p = document.createElement("p"); + p.textContent = key + ": " + User[key]; + output.appendChild(p); + } + } + User.descrivi(); + delete User.eta; + delete User.capelli; + let hr = document.createElement("hr"); + output.appendChild(hr); + for (let key in User) { + if (typeof User[key] !== "function") { + let p = document.createElement("p"); + p.textContent = key + ": " + User[key]; + output.appendChild(p); + } + } +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_74.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_74.js new file mode 100644 index 00000000..c05bf5ca --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_74.js @@ -0,0 +1,46 @@ +/*Realizzare un programma in JS che: +Usando classi e oggetti implementi una classe Automobile con 5 attributi e 3 metodi a vostra scelta +Istanzi 5 oggetti di tipo Automobile e per ciascuno richiamare i 3 metodi. +Consegnare su github il file con nome /cognome/_esercizio_js_56.js*/ + +class Automobile { + constructor(marca, modello, anno, colore, velocita) { + this.marca = marca; + this.modello = modello; + this.anno = anno; + this.colore = colore; + this.velocita = velocita; + } + accelera() { + this.velocita += 10; + console.log(this.modello + " accelera a " + this.velocita + " km/h"); + } + frena() { + this.velocita -= 10; + console.log(this.modello + " rallenta a " + this.velocita + " km/h"); + } + descrivi() { + console.log(this.marca + " " + this.modello + " (" + this.anno + ") - Colore: " + this.colore); + } +} + +let auto1 = new Automobile("Fiat", "Panda", 2018, "Bianco", 50); +let auto2 = new Automobile("BMW", "Serie 3", 2020, "Nero", 80); +let auto3 = new Automobile("Audi", "A4", 2019, "Grigio", 70); +let auto4 = new Automobile("Tesla", "Model 3", 2022, "Rosso", 90); +let auto5 = new Automobile("Ford", "Focus", 2017, "Blu", 60); +auto1.descrivi(); +auto1.accelera(); +auto1.frena(); +auto2.descrivi(); +auto2.accelera(); +auto2.frena(); +auto3.descrivi(); +auto3.accelera(); +auto3.frena(); +auto4.descrivi(); +auto4.accelera(); +auto4.frena(); +auto5.descrivi(); +auto5.accelera(); +auto5.frena(); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_75.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_75.js new file mode 100644 index 00000000..c24009cd --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_75.js @@ -0,0 +1,89 @@ +/*Realizzare un programma in JS: +Usando classi e oggetti implementi tre classi: Animale, Automobile, Poligono, ognuna con 2 metodi diversi e 3 attributi diversi +Ogni classe deve contenere un metodo, oltre ai tre, che si chiama enumera() e deve stampare sulla console tutti i nomi degli attributi dell'oggetto stesso +Istanziare 2 oggetti per ciascuna classe, e su ognuno richiamare il metodo enumera() +Consegnare su github il file con nome /cognome/_esercizio_js_75.js*/ +// cognome_esercizio_js_75.js + +class Animale { + constructor(nome, specie, eta) { + this.nome = nome; + this.specie = specie; + this.eta = eta; + } + verso() { + console.log(this.nome + " fa un verso"); + } + muovi() { + console.log(this.nome + " si muove"); + } + enumera() { + console.log("Attributi Animale:"); + for (let key in this) { + if (typeof this[key] !== "function") { + console.log(key); + } + } + console.log("-------------------"); + } +} + +class Automobile { + constructor(marca, modello, velocita) { + this.marca = marca; + this.modello = modello; + this.velocita = velocita; + } + accelera() { + console.log(this.modello + " accelera"); + } + frena() { + console.log(this.modello + " frena"); + } + enumera() { + console.log("Attributi Automobile:"); + for (let key in this) { + if (typeof this[key] !== "function") { + console.log(key); + } + } + console.log("-------------------"); + } +} + +class Poligono { + constructor(lati, lunghezza, nome) { + this.lati = lati; + this.lunghezza = lunghezza; + this.nome = nome; + } + perimetro() { + console.log("Perimetro:", this.lati * this.lunghezza); + } + descrivi() { + console.log("Poligono:", this.nome); + } + enumera() { + console.log("Attributi Poligono:"); + for (let key in this) { + if (typeof this[key] !== "function") { + console.log(key); + } + } + console.log("-------------------"); + } +} + + +let a1 = new Animale("Leo", "Leone", 5); +let a2 = new Animale("Milo", "Gatto", 2); +let auto1 = new Automobile("Fiat", "Panda", 50); +let auto2 = new Automobile("BMW", "X5", 80); +let p1 = new Poligono(4, 10, "Quadrato"); +let p2 = new Poligono(3, 6, "Triangolo"); +a1.enumera(); +a2.enumera(); +auto1.enumera(); +auto2.enumera(); +p1.enumera(); +p2.enumera(); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_76.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_76.js new file mode 100644 index 00000000..9d1e104f --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_76.js @@ -0,0 +1,49 @@ +/*Realizzare un programma in JS che: +implementi una classe Orario: +con 3 attributi che rappresentano ciascuno un orario (h/m/s) +con un metodo che preso come parametro un oggetto di tipo Orario restituisca: +il numero di ms che separa i due orari +il numero di secondi che separa i due orari +il numero di minuti che separa i due orari +il numero di ore che separa i due orari +fornisca il main che: +verifichi il corretto funzionamento della funzione richiesta stampandone il risultato sulla console +invochi la funzione almeno 2 volte con parametri differenti +Consegnare su github il file con nome /cognome/_esercizio_js_76.js*/ + +class Orario { + constructor(h, m, s) { + this.h = h; + this.m = m; + this.s = s; + } + toSecondi() { + return this.h * 3600 + this.m * 60 + this.s; + } + differenza(orario2) { + + let sec1 = this.toSecondi(); + let sec2 = orario2.toSecondi(); + let diffSecondi = Math.abs(sec1 - sec2); + let diffMs = diffSecondi * 1000; + let diffMinuti = diffSecondi / 60; + let diffOre = diffSecondi / 3600; + return { + millisecondi: diffMs, + secondi: diffSecondi, + minuti: diffMinuti, + ore: diffOre + }; + } +} + +let o1 = new Orario(10, 30, 0); +let o2 = new Orario(12, 0, 0); +let risultato1 = o1.differenza(o2); +console.log("Differenza 1:"); +console.log(risultato1); +let o3 = new Orario(8, 15, 30); +let o4 = new Orario(9, 45, 15); +let risultato2 = o3.differenza(o4); +console.log("Differenza 2:"); +console.log(risultato2); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_77.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_77.js new file mode 100644 index 00000000..e318d13b --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_77.js @@ -0,0 +1,30 @@ +/*Realizzare un programma in JS che: +implementi una classe Fibonacci che: +implementi un metodo che calcoli e restituisca l'ennesimo numero della successione di Fibonacci +fornisca il main che: +verifichi il corretto funzionamento della funzione richiesta stampandone il risultato sulla console +invochi la funzione almeno 5 volte con parametri differenti +Consegnare su github il file con nome /cognome/_esercizio_js_77.js*/ + +class Fibonacci { + calcola(n) { + if (n <= 0) return 0; + if (n === 1) return 1; + let a = 0; + let b = 1; + let risultato; + for (let i = 2; i <= n; i++) { + risultato = a + b; + a = b; + b = risultato; + } + return risultato; + } +} + +let fib = new Fibonacci(); +console.log("F(0) =", fib.calcola(0)); +console.log("F(1) =", fib.calcola(1)); +console.log("F(5) =", fib.calcola(5)); +console.log("F(10) =", fib.calcola(10)); +console.log("F(15) =", fib.calcola(15)); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_78.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_78.js new file mode 100644 index 00000000..993c3ce6 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_78.js @@ -0,0 +1,40 @@ +/*Realizzare un programma in JS: +implementi una classe Orario che: +rappresenta un orario in formato (h/m/s) +fornisca un metodo che presi in ingresso due oggetti di tipo Orario restituisca l'oggetto Orario minore dei tre +fornisca il main che: +verifichi il corretto funzionamento della funzione richiesta stampandone il risultato sulla console +invochi la funzione almeno 2 volte con parametri differenti +Consegnare su github il file con nome /cognome/_esercizio_js_78.js*/ + +class Orario { + constructor(h, m, s) { + this.h = h; + this.m = m; + this.s = s; + } + toSecondi() { + return this.h * 3600 + this.m * 60 + this.s; + } + static minore(o1, o2) { + if (o1.toSecondi() < o2.toSecondi()) { + return o1; + } else { + return o2; + } + } + stampa() { + console.log(this.h + ":" + this.m + ":" + this.s); + } +} + +let o1 = new Orario(10, 30, 0); +let o2 = new Orario(9, 45, 0); +let min1 = Orario.minore(o1, o2); +console.log("Orario minore (test 1):"); +min1.stampa(); +let o3 = new Orario(14, 0, 10); +let o4 = new Orario(14, 0, 5); +let min2 = Orario.minore(o3, o4); +console.log("Orario minore (test 2):"); +min2.stampa(); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_79.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_79.js new file mode 100644 index 00000000..b81db8dc --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_79.js @@ -0,0 +1,104 @@ +/*Π +Realizzare un programma in JS: +realizzi una classe per un oggetto Automobile +L'automobile è caratterizzata da: +Π persone a bordo (un valore compreso tra 0 e 5) +velocità (un valore intero compreso tra 0 e 180) +rapporto di velocità (un valore intero tra 1 e 6) +on/off +Scrivere tutti i metodi che implementano le seguenti funzionalità +impostare il numero di persone a bordo (non posso modificare il numero di persone se velocità e > 0 ) +impostare la velocità: posso inserire un valore qualsiasi, compreso tra 0 e 180, ma che non superi di 30 km/h in eccesso o in difetto la velocità attuale +aumenta rapporto di velocità: cambia la marcia, crescendo di 1 (non posso però superare il valore massimo) +diminuisce rapporto di velocità: cambia la marcia, scalando di 1 (non posso però superare il valore minimo) +pulsante ON/OFF (se Automobile è accesa si spegne, se Automobile è spenta si accende) +Note: +Le variazioni di velocità e marcia sono consentite solo se l'auto è accesa. +Dopo ogni operazione, sullo schermo occorre visualizzare lo stato attuale dell'automobile (persone, velocità, marcia, on/off), tramite apposito metodo +fornisca il main che: +verifichi il corretto funzionamento della classe Automobile instanziandone un oggetto e richiamando tutti +i metodi in modo opportuno, in modo da verificare tutte le possibili combinazioni di funzionamento +L'output è gestito solo dal main +Consegnare su github il file con nome /cognome/_esercizio_js_79.js*/ + +class Automobile { + constructor() { + this.persone = 0; + this.velocita = 0; + this.marcia = 1; + this.accesa = false; + } + stampaStato() { + console.log("Persone:", this.persone, + "| Velocità:", this.velocita, + "| Marcia:", this.marcia, + "| Accesa:", this.accesa); + console.log("----------------------"); + } + toggle() { + this.accesa = !this.accesa; + } + setPersone(n) { + if (this.velocita > 0) { + console.log("ERRORE: non puoi cambiare persone in movimento"); + return; + } + if (n >= 0 && n <= 5) { + this.persone = n; + } + } + setVelocita(v) { + if (!this.accesa) { + console.log("ERRORE: auto spenta"); + return; + } + if (v >= 0 && v <= 180 && Math.abs(v - this.velocita) <= 30) { + this.velocita = v; + } else { + console.log("ERRORE: variazione velocità non valida"); + } + } + aumentaMarcia() { + if (!this.accesa) { + console.log("ERRORE: auto spenta"); + return; + } + if (this.marcia < 6) { + this.marcia++; + } + } + diminuisciMarcia() { + if (!this.accesa) { + console.log("ERRORE: auto spenta"); + return; + } + if (this.marcia > 1) { + this.marcia--; + } + } +} + +let auto = new Automobile(); +auto.stampaStato(); +auto.toggle(); +auto.stampaStato(); +auto.setPersone(3); +auto.stampaStato(); +auto.setVelocita(20); +auto.stampaStato(); +auto.setPersone(5); +auto.stampaStato(); +auto.aumentaMarcia(); +auto.stampaStato(); +auto.aumentaMarcia(); +auto.stampaStato(); +auto.setVelocita(100); +auto.stampaStato(); +auto.diminuisciMarcia(); +auto.stampaStato(); +auto.setVelocita(0); +auto.stampaStato(); +auto.setPersone(5); +auto.stampaStato(); +auto.toggle(); +auto.stampaStato(); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A01.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A01.js new file mode 100644 index 00000000..8627077d --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A01.js @@ -0,0 +1,43 @@ +class Libro { + // Costruttore per inizializzare le proprietà + constructor(titolo, autore, anno) { + this.titolo = titolo; + this.autore = autore; + this.anno = anno; + } + + // Metodo per restituire le informazioni del libro formattate + dettagli() { + return `Titolo: "${this.titolo}", Autore: ${this.autore}, Anno: ${this.anno}`; + } + + // Metodo statico per confrontare l'anno di due libri + static comparareAnno(l1, l2) { + if (l1.anno > l2.anno) { + return l1; + } else if (l2.anno > l1.anno) { + return l2; + } else { + return "I libri sono stati pubblicati nello stesso anno."; + } + } +} + +// --- Esempio di utilizzo pratico --- + +// Creazione di due istanze (oggetti) della classe Libro +const libro1 = new Libro("Il Signore degli Anelli", "J.R.R. Tolkien", 1954); +const libro2 = new Libro("Harry Potter e la Pietra Filosofale", "J.K. Rowling", 1997); + +// Test del metodo dettagli() +console.log(libro1.dettagli()); +// Output: Titolo: "Il Signore degli Anelli", Autore: J.R.R. Tolkien", Anno: 1954 + +console.log(libro2.dettagli()); +// Output: Titolo: "Harry Potter e la Pietra Filosofale", Autore: J.K. Rowling", Anno: 1997 + +// Test del metodo statico comparareAnno() +const piuRecente = Libro.comparareAnno(libro1, libro2); +console.log("Il libro più recente è:"); +console.log(piuRecente.dettagli()); +// Output: Titolo: "Harry Potter e la Pietra Filosofale", Autore: J.K. Rowling", Anno: 1997 diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A02.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A02.js new file mode 100644 index 00000000..4ac318f3 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A02.js @@ -0,0 +1,12 @@ +// 1. Definiamo una stringa JSON che rappresenta un libro +const jsonLibro = '{"titolo": "Il nome della rosa", "autore": "Umberto Eco", "anno": 1980}'; + +// 2. Usiamo JSON.parse() per convertire la stringa in un oggetto JavaScript +const libroOggetto = JSON.parse(jsonLibro); + +// 3. Usiamo il destructuring per estrarre titolo e autore +const { titolo, autore } = libroOggetto; + +// 4. Stampiamo i risultati +console.log(`Titolo: ${titolo}`); +console.log(`Autore: ${autore}`); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A03.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A03.js new file mode 100644 index 00000000..91b67cd1 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A03.js @@ -0,0 +1,23 @@ +const elaboraPersone = (persone) => { + return persone + // 1. Filtra le persone con più di 18 anni + .filter(persona => persona.età > 18) + + // 2. Ordina per età in ordine crescente + .sort((a, b) => a.età - b.età) + + // 3. Restituisce solo nome e cognome + .map(persona => ({ + nome: persona.nome, + cognome: persona.cognome + })); +}; + +// Esempio di utilizzo: +const dati = [ + { nome: "Marco", cognome: "Rossi", età: 25 }, + { nome: "Anna", cognome: "Verdi", età: 17 }, + { nome: "Luca", cognome: "Bianchi", età: 20 } +]; + +console.log(elaboraPersone(dati)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A04.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A04.js new file mode 100644 index 00000000..c51775c1 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A04.js @@ -0,0 +1,29 @@ +// Definizione della classe base +class Animale { + constructor(nome) { + this.nome = nome; + } + + saluta() { + console.log(`Ciao, io sono ${this.nome}!`); + } +} + +// Definizione della classe derivata che estende Animale +class Cane extends Animale { + constructor(nome) { + // Chiama il costruttore della classe genitore + super(nome); + } + + abbaia() { + console.log("Bau bau!"); + } +} + +// Creazione di un'istanza di Cane +const mioCane = new Cane("Fido"); + +// Chiamata ai metodi +mioCane.saluta(); // Metodo ereditato da Animale +mioCane.abbaia(); // Metodo specifico di Cane diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A05.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A05.js new file mode 100644 index 00000000..85e9742a --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A05.js @@ -0,0 +1,30 @@ +// 1. Crea una Map che memorizza il nome di un prodotto e il suo prezzo +const prodotti = new Map(); +prodotti.set("Laptop", 1000); +prodotti.set("Smartphone", 500); +prodotti.set("Tablet", 300); + +// 2. Funzione che applica uno sconto del 10% e restituisce un nuovo Map +function applicaSconto(mappaOriginale) { + const mappaScontata = new Map(); + + mappaOriginale.forEach((prezzo, nome) => { + const prezzoScontato = prezzo * 0.9; // Applica lo sconto del 10% + mappaScontata.set(nome, prezzoScontato); + }); + + return mappaScontata; +} + +const prodottiScontati = applicaSconto(prodotti); + +// 3. Crea un Set per memorizzare i nomi dei prodotti unici +const nomiUnici = new Set(); +prodotti.forEach((prezzo, nome) => { + nomiUnici.add(nome); +}); + +// Test dei risultati +console.log("Prodotti Originali:", prodotti); +console.log("Prodotti Scontati (10%):", prodottiScontati); +console.log("Nomi Prodotti (Set):", nomiUnici); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A06.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A06.js new file mode 100644 index 00000000..9e254572 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A06.js @@ -0,0 +1,30 @@ +/** + * Funzione che formatta le info di un prodotto da una stringa JSON + * @param {string} jsonProdotto - Stringa in formato JSON + * @returns {string} - Stringa formattata + */ +function formattaProdotto(jsonProdotto) { + // Trasforma la stringa JSON in un oggetto JavaScript + const prodotto = JSON.parse(jsonProdotto); + + // Estrae le proprietà + const { nome, categoria, prezzo } = prodotto; + + // Restituisce la stringa formattata + return `Prodotto: ${nome} | Categoria: ${categoria} | Prezzo: €${prezzo.toFixed(2)}`; +} + +// --- ESEMPIO DI UTILIZZO --- + +const datiEsempio = { + nome: "Laptop XPS", + categoria: "Elettronica", + prezzo: 1299.99 +}; + +// Creiamo una stringa JSON (come richiesto dai metodi indicati) +const stringaJSON = JSON.stringify(datiEsempio); + +// Chiamata alla funzione +console.log(formattaProdotto(stringaJSON)); +// Output: "Prodotto: Laptop XPS | Categoria: Elettronica | Prezzo: €1299.99" diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A07.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A07.js new file mode 100644 index 00000000..faf5d69a --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A07.js @@ -0,0 +1,20 @@ +const applicaSconto = (prodotti) => { + return prodotti.map(({ nome, prezzo, ...resto }) => { + // Destructuring per estrarre nome e prezzo + // Spread operator per creare un nuovo oggetto con il prezzo aggiornato + return { + ...resto, + nome, + prezzo: prezzo * 0.9 // Applica lo sconto del 10% + }; + }); +}; + +// Esempio di utilizzo: +const inventario = [ + { nome: "Smartphone", prezzo: 500, categoria: "Elettronica" }, + { nome: "Libro", prezzo: 20, categoria: "Cultura" } +]; + +const inventarioScontato = applicaSconto(inventario); +console.log(inventarioScontato); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A08.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A08.js new file mode 100644 index 00000000..38d8e69c --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A08.js @@ -0,0 +1,28 @@ +// Classe base Persona +class Persona { + constructor(nome, cognome) { + this.nome = nome; + this.cognome = cognome; + } + + saluta() { + return `Ciao, sono ${this.nome} ${this.cognome}.`; + } +} + +// Classe Studente che estende Persona +class Studente extends Persona { + constructor(nome, cognome, voti = []) { + // Richiamo il costruttore della classe genitore + super(nome, cognome); + this.voti = voti; + } + + mediaVoti() { + if (this.voti.length === 0) return 0; + + // Utilizzo di Array.reduce() per calcolare la somma + const somma = this.voti.reduce((acc, voto) => acc + voto, 0); + return somma / this.voti.length; + } +} diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A09.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A09.js new file mode 100644 index 00000000..8ee96b02 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A09.js @@ -0,0 +1,30 @@ +// 1. Funzione fetchDati() che simula un'API asincrona +function fetchDati() { + return new Promise((resolve, reject) => { + console.log("Recupero dati in corso..."); + + setTimeout(() => { + // Simula un successo (puoi cambiare in false per testare l'errore) + const successo = true; + + if (successo) { + resolve({ nome: "Mario", eta: 30 }); + } else { + reject("Errore durante il recupero dei dati."); + } + }, 2000); // Ritardo di 2 secondi + }); +} + +// 2. Funzione per invocare fetchDati con async/await e gestione errori +async function gestisciDati() { + try { + const dati = await fetchDati(); + console.log("Dati ricevuti:", dati); + } catch (errore) { + console.error("Si è verificato un errore:", errore); + } +} + +// Esecuzione +gestisciDati(); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A10.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A10.js new file mode 100644 index 00000000..9ef2562b --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A10.js @@ -0,0 +1,22 @@ +function elaboraPersone(jsonStrings) { + // 1. Converte ogni stringa JSON in un oggetto JavaScript + const persone = jsonStrings.map(jsonStr => JSON.parse(jsonStr)); + + // 2. Ordina le persone per età (crescente) + persone.sort((a, b) => a.età - b.età); + + // 3. Filtra le persone con età maggiore di 30 anni + const over30 = persone.filter(persona => persona.età > 30); + + // Restituisce il risultato in formato JSON + return JSON.stringify(over30); +} + +// Esempio di utilizzo: +const input = [ + '{"nome": "Luca", "cognome": "Rossi", "età": 25}', + '{"nome": "Anna", "cognome": "Verdi", "età": 42}', + '{"nome": "Marco", "cognome": "Neri", "età": 35}' +]; + +console.log(elaboraPersone(input)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A11.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A11.html new file mode 100644 index 00000000..e22c3502 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A11.html @@ -0,0 +1,122 @@ + + + + + + Lista di Parole con Ordinamento e Filtraggio + + + + +
+

La mia Lista di Parole

+ + + + + +
    +
    + + + + + diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A12.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A12.html new file mode 100644 index 00000000..c1341899 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A12.html @@ -0,0 +1,158 @@ + + + + + + A12: Contatore di Occorrenze delle Parole + + + + +
    +

    Contatore di Parole

    + + +
    + + +
    + +
    Il risultato apparirà qui...
    +
    + + + + + diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A13.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A13.html new file mode 100644 index 00000000..fb679ae7 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A13.html @@ -0,0 +1,86 @@ + + + + + A13: Esercizio con Errori + + + + +
    +

    Filtro Prodotti (Versione con Errori)

    + +
    + + +
    + +
      +
      + + + + + diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A14.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A14.html new file mode 100644 index 00000000..f6b95499 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A14.html @@ -0,0 +1,191 @@ + + + + + + A14: Tabella Dinamica Nomi e Cognomi + + + + +
      +

      Registro Utenti

      + + +
      + + + +
      + + + + + + + + + + + + + +
      NomeCognome
      Nessun dato inserito nella tabella
      +
      + + + + + diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A15.html b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A15.html new file mode 100644 index 00000000..c18f097d --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_A15.html @@ -0,0 +1,206 @@ + + + + + + A15: Gestione Prodotti con Aggiunta e Rimozione + + + + +
      +

      Lista della Spesa

      + + +
      +
      + + +
      +
      + + +
      +
      + +
        +
        + + + + + diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B01.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B01.js new file mode 100644 index 00000000..4674b901 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B01.js @@ -0,0 +1,21 @@ +// Esempio di input +let numero = 42.5; +let stringa = " "; + +// 1. Verifica se il numero è un intero o un decimale +if (Number.isInteger(numero)) { + console.log("Il numero è un intero."); +} else { + console.log("Il numero è un decimale."); +} + +// 2. Controlla se la stringa è vuota (usando trim() per ignorare gli spazi) +if (stringa.trim() === "") { + console.log("La stringa è vuota."); +} else { + console.log("La stringa non è vuota."); +} + +// 3. Stampa il tipo di dato di ciascun input +console.log("Tipo del primo input:", typeof numero); +console.log("Tipo del secondo input:", typeof stringa); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B02.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B02.js new file mode 100644 index 00000000..cffa7aff --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B02.js @@ -0,0 +1,15 @@ +function formattaStringa(testo) { + if (!testo) return ""; // Gestisce il caso di stringa vuota + + // Prende il primo carattere e lo rende maiuscolo + const primaLettera = testo.charAt(0).toUpperCase(); + + // Prende il resto della stringa e lo rende minuscolo + const restoDellaStringa = testo.slice(1).toLowerCase(); + + return primaLettera + restoDellaStringa; +} + +// Esempio di utilizzo: +console.log(formattaStringa("jAVaSCRIPT")); // Output: "Javascript" +console.log(formattaStringa("mela")); // Output: "Mela" diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B03.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B03.js new file mode 100644 index 00000000..6ea48a10 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B03.js @@ -0,0 +1,19 @@ +function calcolaOperazioniArray(numeri) { + // Calcola la somma utilizzando Array.reduce() + const somma = numeri.reduce((acc, curr) => acc + curr, 0); + + // Calcola la media utilizzando la somma e Array.length + const media = numeri.length > 0 ? somma / numeri.length : 0; + + return { + somma: somma, + media: media + }; +} + +// Esempio di utilizzo: +const dati = [10, 20, 30, 40]; +const risultato = calcolaOperazioniArray(dati); + +console.log("Somma:", risultato.somma); // 100 +console.log("Media:", risultato.media); // 25 diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B04.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B04.js new file mode 100644 index 00000000..a096783b --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B04.js @@ -0,0 +1,14 @@ +function verificaTipo(variabile) { + const tipo = typeof variabile; + + if (tipo !== "undefined") { + console.log("Il tipo di dato è " + tipo); + } else { + console.log("Non è un tipo valido"); + } +} + +// Esempi di utilizzo: +verificaTipo(10); // Il tipo di dato è number +verificaTipo("Ciao"); // Il tipo di dato è string +verificaTipo(undefined); // Non è un tipo valido diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B05.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B05.js new file mode 100644 index 00000000..b57358d3 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_B05.js @@ -0,0 +1,9 @@ +function concatenaStringhe(stringa1, stringa2) { + // Il metodo concat() unisce il testo di due o più stringhe + // e restituisce una nuova stringa. + return stringa1.concat(stringa2); +} + +// Esempio di utilizzo: +const risultato = concatenaStringhe("Ciao ", "Mondo!"); +console.log(risultato); // Output: "Ciao Mondo!" diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I01.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I01.js new file mode 100644 index 00000000..1967fec2 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I01.js @@ -0,0 +1,18 @@ +function dividiPerDue(input) { + try { + // Verifica se l'input non è un numero valido + if (isNaN(input)) { + throw new Error("L'input non è un numero valido."); + } + + // Esegue la divisione se l'input è valido + return input / 2; + } catch (error) { + // Gestisce l'errore se l'input non è numerico + return error.message; + } +} + +// Esempi di utilizzo: +console.log(dividiPerDue(10)); // Output: 5 +console.log(dividiPerDue("ciao")); // Output: L'input non è un numero valido. diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I02.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I02.js new file mode 100644 index 00000000..8984f1c8 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I02.js @@ -0,0 +1,39 @@ +function formattaPersona(persona) { + // Utilizzo di Object.keys() per dimostrare l'estrazione delle chiavi (anche se non strettamente necessario per il calcolo) + const chiavi = Object.keys(persona); + + const nome = persona.nome; + const cognome = persona.cognome; + const dataNascitaStr = persona.dataDiNascita; // Formato "YYYY-MM-DD" + + // Utilizzo di String.split() per separare anno, mese e giorno + const partiData = dataNascitaStr.split("-"); + const annoNascita = parseInt(partiData[0]); + const meseNascita = parseInt(partiData[1]) - 1; // I mesi in JS sono 0-indexed (0 = Gennaio) + const giornoNascita = parseInt(partiData[2]); + + // Utilizzo di Date() per ottenere la data corrente e quella di nascita + const oggi = new Date(); + const dataNascita = new Date(annoNascita, meseNascita, giornoNascita); + + // Calcolo dell'età utilizzando getFullYear(), getMonth() e getDate() + let eta = oggi.getFullYear() - dataNascita.getFullYear(); + const meseCorrente = oggi.getMonth(); + const giornoCorrente = oggi.getDate(); + + // Verifica se il compleanno è già avvenuto quest'anno + if (meseCorrente < meseNascita || (meseCorrente === meseNascita && giornoCorrente < giornoNascita)) { + eta--; + } + + return `${nome} ${cognome}, Età: ${eta}`; +} + +// Esempio di utilizzo: +const utente = { + nome: "Mario", + cognome: "Rossi", + dataDiNascita: "1990-05-21" +}; + +console.log(formattaPersona(utente)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I03.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I03.js new file mode 100644 index 00000000..8764c898 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I03.js @@ -0,0 +1,29 @@ +// 1. Crea una Map per nomi e voti +const registroStudenti = new Map(); + +// 2. Aggiungi almeno 5 studenti con Map.set() +registroStudenti.set("Alice", 28); +registroStudenti.set("Bruno", 24); +registroStudenti.set("Carla", 30); +registroStudenti.set("Dario", 28); +registroStudenti.set("Elena", 22); + +// 3. Usa un ciclo for...of per stampare nomi e voti +console.log("Elenco Studenti:"); +for (const [nome, voto] of registroStudenti) { + console.log(`${nome}: ${voto}`); +} + +// 4. Crea un Set con i voti unici degli studenti +const votiUnici = new Set(); + +for (const [nome, voto] of registroStudenti) { + // Uso opzionale di Map.has() e Map.get() come da lista metodi + if (registroStudenti.has(nome)) { + const votoStudente = registroStudenti.get(nome); + votiUnici.add(votoStudente); + } +} + +console.log("\nVoti unici registrati:"); +console.log(votiUnici); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I04.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I04.js new file mode 100644 index 00000000..3ad23c4e --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I04.js @@ -0,0 +1,9 @@ +function filtraMultipliDiTre(numeri) { + return numeri.filter(numero => numero % 3 === 0); +} + +// Esempio di utilizzo: +const arrayOriginale = [1, 3, 5, 9, 12, 14, 18, 20]; +const risultati = filtraMultipliDiTre(arrayOriginale); + +console.log(risultati); // Output: [3, 9, 12, 18] diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I05.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I05.js new file mode 100644 index 00000000..9594269e --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I05.js @@ -0,0 +1,19 @@ +// Creazione dell'oggetto libro iniziale +const libro = { + titolo: "Il Signore degli Anelli", + autore: "J.R.R. Tolkien", + anno: 1954, + genere: "Fantasy", + + // Funzione per modificare il genere + cambiaGenere: function(nuovoGenere) { + // Uso di Object.assign() per aggiornare la proprietà genere + Object.assign(this, { genere: nuovoGenere }); + return `Il nuovo genere del libro è: ${this.genere}`; + } +}; + +// Esempio di utilizzo +console.log(libro.genere); // Output: Fantasy +console.log(libro.cambiaGenere("Avventura")); // Output: Il nuovo genere del libro è: Avventura +console.log(libro.genere); // Output: Avventura diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I06.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I06.js new file mode 100644 index 00000000..06dfe064 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I06.js @@ -0,0 +1,15 @@ +// 1. Crea un array di oggetti con nome ed età +const persone = [ + { nome: "Alice", eta: 30 }, + { nome: "Bob", eta: 25 }, + { nome: "Charlie", eta: 35 } +]; + +// 2. Scrivi una funzione che ordina l'array in ordine crescente di età +function ordinaPerEta(arrayPersone) { + // Il metodo sort() modifica l'array originale + return arrayPersone.sort((a, b) => a.eta - b.eta); +} + +// Esecuzione e visualizzazione del risultato +console.log(ordinaPerEta(persone)); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I07.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I07.js new file mode 100644 index 00000000..07fdab46 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I07.js @@ -0,0 +1,16 @@ +function filtraLibriPerAnno(libri, annoRiferimento) { + // Il metodo filter crea un nuovo array con i libri + // che hanno la proprietà 'anno' maggiore di quello fornito + return libri.filter(libro => libro.anno > annoRiferimento); +} + +// Esempio di utilizzo: +const catalogo = [ + { titolo: "Il nome della rosa", autore: "Umberto Eco", anno: 1980 }, + { titolo: "Il problema dei tre corpi", autore: "Liu Cixin", anno: 2008 }, + { titolo: "Dune", autore: "Frank Herbert", anno: 1965 }, + { titolo: "Circe", autore: "Madeline Miller", anno: 2018 } +]; + +const libriRecenti = filtraLibriPerAnno(catalogo, 2000); +console.log(libriRecenti); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I08.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I08.js new file mode 100644 index 00000000..0b684968 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I08.js @@ -0,0 +1,20 @@ +function confrontaOggetti(obj1, obj2) { + const keys1 = Object.keys(obj1); + const keys2 = Object.keys(obj2); + + // Se hanno un numero diverso di proprietà, non possono essere uguali + if (keys1.length !== keys2.length) { + return false; + } + + // Verifica che ogni proprietà di obj1 sia uguale a quella di obj2 + return keys1.every(key => obj1[key] === obj2[key]); +} + +// Esempio d'uso: +const libroA = { titolo: "Il nome della rosa", autore: "Umberto Eco", anno: 1980 }; +const libroB = { titolo: "Il nome della rosa", autore: "Umberto Eco", anno: 1980 }; +const libroC = { titolo: "1984", autore: "George Orwell", anno: 1949 }; + +console.log(confrontaOggetti(libroA, libroB)); // true +console.log(confrontaOggetti(libroA, libroC)); // false diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I09.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I09.js new file mode 100644 index 00000000..767b130b --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I09.js @@ -0,0 +1,32 @@ +function ricercaProdotti(categoriaCercata) { + // 1. Creazione della Map + const catalogo = new Map(); + + // 2. Popolamento della Map usando Map.set() + catalogo.set("elettronica", [ + { nome: "Smartphone", prezzo: 500 }, + { nome: "Laptop", prezzo: 1200 } + ]); + + catalogo.set("abbigliamento", [ + { nome: "T-shirt", prezzo: 20 }, + { nome: "Jeans", prezzo: 50 } + ]); + + catalogo.set("alimentari", [ + { nome: "Pane", prezzo: 2 }, + { nome: "Latte", prezzo: 1.5 } + ]); + + // 3. Ricerca della categoria + // Map.has() verifica se la chiave esiste + if (catalogo.has(categoriaCercata)) { + // Map.get() restituisce il valore (l'array di prodotti) + return catalogo.get(categoriaCercata); + } else { + return "Categoria non trovata."; + } +} + +// Esempio d'uso: +console.log(ricercaProdotti("elettronica")); diff --git a/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I10.js b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I10.js new file mode 100644 index 00000000..45e3d1fc --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/bianchi_esercizio_js_I10.js @@ -0,0 +1,26 @@ +/** + * Ordina un array di oggetti per età (crescente) + * e per nome (alfabetico) in caso di parità. + */ +function ordinaPersone(persone) { + // Copiamo l'array con map() per evitare di modificare l'originale + return persone.map(p => p).sort((a, b) => { + // 1. Confronto per età + if (a.eta !== b.eta) { + return a.eta - b.eta; + } + // 2. Se le età sono uguali, confronto per nome + return a.nome.localeCompare(b.nome); + }); +} + +// Esempio d'uso: +const lista = [ + { nome: "Marco", eta: 25 }, + { nome: "Anna", eta: 22 }, + { nome: "Luca", eta: 25 }, + { nome: "Giulia", eta: 22 } +]; + +const risultato = ordinaPersone(lista); +console.log(risultato); diff --git a/TPSI/bianchi_emanuele/JavaScript/es59.js b/TPSI/bianchi_emanuele/JavaScript/es59.js new file mode 100644 index 00000000..e69de29b diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio38.js b/TPSI/bianchi_emanuele/JavaScript/esercizio38.js new file mode 100644 index 00000000..9af64e0b --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio38.js @@ -0,0 +1,4 @@ +function reverseArray(array) { + return array.slice().sort((a,b) => b - a); +} +console.log((reverseArray([10, 50, 5]))); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio39.js b/TPSI/bianchi_emanuele/JavaScript/esercizio39.js new file mode 100644 index 00000000..df511f7a --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio39.js @@ -0,0 +1,6 @@ +function sommaInversa(arr) { + const somma = arr.reduce((sum, n) => sum + n, 0); + const inverso = arr.slice().sort((a,b) => b - a); + return [somma, ...inverso]; +} +console.log((sommaInversa([1,2,3]))); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio40.js b/TPSI/bianchi_emanuele/JavaScript/esercizio40.js new file mode 100644 index 00000000..d4663756 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio40.js @@ -0,0 +1,6 @@ +function arrayUguali(arr1, arr2) { + return arr1.length === arr2.length && + (arr1) === (arr2); +} +console.log(arrayUguali([1,2,3], [1,2,3])); +console.log(arrayUguali([1,2,3], [1,3,3])); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio41.js b/TPSI/bianchi_emanuele/JavaScript/esercizio41.js new file mode 100644 index 00000000..0967ac28 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio41.js @@ -0,0 +1,4 @@ +function creaArrayRipetuto(N, C) { + return Array(N).fill(C); +} +console.log((creaArrayRipetuto(3, 'a'))); diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio42.js b/TPSI/bianchi_emanuele/JavaScript/esercizio42.js new file mode 100644 index 00000000..fc408f32 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio42.js @@ -0,0 +1,7 @@ +function spostaElemento(arr, N, M) { + const risultato = [...arr]; + const elemento = risultato.splice(N, 1)[0]; + risultato.splice(M, 0, elemento); + return risultato; +} +console.log((spostaElemento([10,20,30,40,50], 0, 2))); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio43.js b/TPSI/bianchi_emanuele/JavaScript/esercizio43.js new file mode 100644 index 00000000..1b78dc7a --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio43.js @@ -0,0 +1,4 @@ +function rimuoviNumero(arr, N) { + return arr.filter(x => x !== N); +} +console.log((rimuoviNumero([1,2,3,4], 3))); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio44.js b/TPSI/bianchi_emanuele/JavaScript/esercizio44.js new file mode 100644 index 00000000..03a936ec --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio44.js @@ -0,0 +1,9 @@ +var library = [ + { author: 'Bill Gates', title: 'The Road Ahead', libraryID: 1254 }, + { author: 'Steve Jobs', title: 'Walter Isaacson', libraryID: 4264 }, + { author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', libraryID: 3245 } +]; +function ordinaLibraryDecrescente(library) { + return library.slice().sort((a,b) => b.title > a.title ? -1 : 1); +} +console.log((ordinaLibraryDecrescente(library))); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio45.js b/TPSI/bianchi_emanuele/JavaScript/esercizio45.js new file mode 100644 index 00000000..881792d8 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio45.js @@ -0,0 +1,4 @@ +function estraiMaiuscole(stringa) { + return stringa.split('').filter(c => c >= 'A' && c <= 'Z'); +} +console.log((estraiMaiuscole("ccHEewLtfL20q sWrds0gtvfdcRwLffDff"))); \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio46.js b/TPSI/bianchi_emanuele/JavaScript/esercizio46.js new file mode 100644 index 00000000..5cb8f663 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio46.js @@ -0,0 +1,3 @@ +function prodottoQuadrati(arr) { + return a.reduce((acc, current) => acc*Math.paw(current,2),1); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio47.js b/TPSI/bianchi_emanuele/JavaScript/esercizio47.js new file mode 100644 index 00000000..8103ef9f --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio47.js @@ -0,0 +1,3 @@ +function primiElementi(array, n) { + return array.slice(0, n); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio48.js b/TPSI/bianchi_emanuele/JavaScript/esercizio48.js new file mode 100644 index 00000000..3486694e --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio48.js @@ -0,0 +1,3 @@ +function ultimiElementi(array, n) { + return array.slice(-n); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio49.js b/TPSI/bianchi_emanuele/JavaScript/esercizio49.js new file mode 100644 index 00000000..90592d79 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio49.js @@ -0,0 +1,3 @@ +function azzeraMultipli(array, n) { + return array.map(num => num % n === 0 ? 0 : num); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio50.js b/TPSI/bianchi_emanuele/JavaScript/esercizio50.js new file mode 100644 index 00000000..5eae4801 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio50.js @@ -0,0 +1,13 @@ +function isPrimo(num) { + if (num <= 1) return true; + for (let i = 2; i <= Math.sqrt(num); i++) { + if (num % i === 0) return false; + } + return true; +} + +function ordinaPrimi(array) { + const primi = array.filter(n => isPrimo(n)); + const altri = array.filter(n => !isPrimo(n)); + return [...primi, ...altri]; +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio51.js b/TPSI/bianchi_emanuele/JavaScript/esercizio51.js new file mode 100644 index 00000000..a482a007 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio51.js @@ -0,0 +1,3 @@ +function binarioADecimale(stringaBinaria) { + return /*nulla*/; +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio52.js b/TPSI/bianchi_emanuele/JavaScript/esercizio52.js new file mode 100644 index 00000000..53e241ab --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio52.js @@ -0,0 +1,9 @@ +function generaCasuale() { + const caratteri = "abcdefghijklmnopqrstuvwxyz0123456789"; + let risultato = []; + for (let i = 0; i < 7; i++) { + const indice = Math.floor(Math.random() * caratteri.length); + risultato.push(caratteri[indice]); + } + return risultato; +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio53.js b/TPSI/bianchi_emanuele/JavaScript/esercizio53.js new file mode 100644 index 00000000..5ad587f4 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio53.js @@ -0,0 +1,7 @@ +function invertiSottoArray(array, m) { + const parte1 = array.slice(0, m); + const parte2 = array.slice(m + 1); + return [...parte2, ...parte1]; +} + +// Test slide: [1,2,3,4,5,6,7], M=3 -> [5,6,7, 1,2,3,4] \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio54.js b/TPSI/bianchi_emanuele/JavaScript/esercizio54.js new file mode 100644 index 00000000..ec436907 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio54.js @@ -0,0 +1,5 @@ +function arrayCasualeInvertito(n) { + let array = Array.from({ length: n }, () => Math.floor(Math.random() * 20)); + console.log("Originale:", array); + console.log("Invertito:", [...array].reverse()); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio55.js b/TPSI/bianchi_emanuele/JavaScript/esercizio55.js new file mode 100644 index 00000000..b20ab623 --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio55.js @@ -0,0 +1,3 @@ +function rimuoviDuplicati(array) { + return /* nulla*/ ; +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio56.js b/TPSI/bianchi_emanuele/JavaScript/esercizio56.js new file mode 100644 index 00000000..fe17a48f --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio56.js @@ -0,0 +1,4 @@ +function calcolaMediaEta(studenti) { + const somma = studenti.reduce((acc, studente) => acc + studente.eta, 0); + return somma / studenti.length; +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio57.js b/TPSI/bianchi_emanuele/JavaScript/esercizio57.js new file mode 100644 index 00000000..1d7d6ddf --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio57.js @@ -0,0 +1,3 @@ +function estraiNomi(listaStudenti) { + return listaStudenti.map(studente => studente.nome); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/JavaScript/esercizio58.js b/TPSI/bianchi_emanuele/JavaScript/esercizio58.js new file mode 100644 index 00000000..4bb6fb3c --- /dev/null +++ b/TPSI/bianchi_emanuele/JavaScript/esercizio58.js @@ -0,0 +1,3 @@ +function filtraIntervallo(array, n, m) { + return array.filter(valore => valore >= n && valore <= m); +} \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/gioco/index.html b/TPSI/bianchi_emanuele/gioco/index.html new file mode 100644 index 00000000..c9fd85bd --- /dev/null +++ b/TPSI/bianchi_emanuele/gioco/index.html @@ -0,0 +1,54 @@ + + + + + + Impiccato - Lavagna + + + +
        + +

        🪦 IMPICCATO 🪦

        + + +
        + + +
        + + + + + + +
        + + + + diff --git a/TPSI/bianchi_emanuele/gioco/l'impiccato.txt b/TPSI/bianchi_emanuele/gioco/l'impiccato.txt new file mode 100644 index 00000000..5f037508 --- /dev/null +++ b/TPSI/bianchi_emanuele/gioco/l'impiccato.txt @@ -0,0 +1,13 @@ +cremi e spiegami passo passo la creazione del gioco l'impiccato in modo facile in javascript: + deve contenere: + lettere come se fossero una tastiera + no parole accentate + no parole apostrofate + sfondo di lavagna + scritte come se fossero fatte a gesso + parole random + modalità uno o due giocatore/i + se uno: il computer mette la parola random e il giocatore la deve indovinare + se due un giocatore mette la parola e l'altro la deve indovinare + +il lavoro dev'essere fatto da uno studente con le basi di JS \ No newline at end of file diff --git a/TPSI/bianchi_emanuele/gioco/script.js b/TPSI/bianchi_emanuele/gioco/script.js new file mode 100644 index 00000000..44f3a4d1 --- /dev/null +++ b/TPSI/bianchi_emanuele/gioco/script.js @@ -0,0 +1,216 @@ +// === PAROLE ITALIANE SEMPLICI (NO ACCENTI, NO APOSTROFI) === +const parole = [ + "cane", "gatto", "casa", "albero", "fiore", + "sole", "luna", "stella", "mare", "montagna", + "pizza", "pasta", "pane", "latte", "acqua", + "roma", "milano", "napoli", "libro", "scuola" +]; + +// === VARIABILI GIOCO === +let parolaScelta = ""; +let parolaIndovinata = []; +let tentativiRimanenti = 6; +let lettereUsate = []; +let modalitaGioco = 1; // 1 = un giocatore, 2 = due giocatori + +// === AVVIO GIOCO === +document.addEventListener('DOMContentLoaded', function() { + aggiungiEventi(); + mostraSelezioneModalita(); +}); + +// === AGGIUNGI EVENTI === +function aggiungiEventi() { + document.getElementById('mode1-btn').addEventListener('click', () => scegliModalita(1)); + document.getElementById('mode2-btn').addEventListener('click', () => scegliModalita(2)); + document.getElementById('start-game-btn').addEventListener('click', iniziaGiocoDueGiocatori); + document.getElementById('reset-button').addEventListener('click', resettaGioco); +} + +// === SCEGLI MODALITÀ === +function scegliModalita(modalita) { + modalitaGioco = modalita; + + // Aggiorna pulsanti + document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active')); + document.getElementById(`mode${modalita}-btn`).classList.add('active'); + + if (modalita === 1) { + // Un giocatore: inizia subito + document.getElementById('player2-input').style.display = 'none'; + document.getElementById('game-container').style.display = 'block'; + document.getElementById('mode-selection').style.display = 'none'; + giocoParolaCasuale(); + } else { + // Due giocatori: mostra input + document.getElementById('player2-input').style.display = 'block'; + document.getElementById('game-container').style.display = 'none'; + } +} + +// === GIOCO UN GIOCATORE (PAROLA CASUALE) === +function giocoParolaCasuale() { + // Sceglie parola a caso + parolaScelta = parole[Math.floor(Math.random() * parole.length)]; + iniziaPartita(); +} + +// === GIOCO DUE GIOCATORI === +function iniziaGiocoDueGiocatori() { + let parolaInserita = document.getElementById('custom-word').value.trim(); + + // Controlla lunghezza + if (parolaInserita.length < 3 || parolaInserita.length > 12) { + alert('Parola tra 3 e 12 lettere!'); + return; + } + + // Controlla accenti e apostrofi (semplice) + if (!/^[a-zA-Z]+$/.test(parolaInserita)) { + alert('Solo lettere normali (no accenti/apost)!'); + return; + } + + parolaScelta = parolaInserita.toLowerCase(); + document.getElementById('custom-word').value = ''; + iniziaPartita(); + + // Nascondi input + document.getElementById('player2-input').style.display = 'none'; + document.getElementById('game-container').style.display = 'block'; +} + +// === INIZIA PARTITA === +function iniziaPartita() { + // Crea underscore per ogni lettera + parolaIndovinata = Array(parolaScelta.length).fill('_'); + tentativiRimanenti = 6; + lettereUsate = []; + + mostraParola(); + aggiornaTeschi(); + mostraLettereUsate(); + creaTastiera(); +} + +// === MOSTRA PAROLA === +function mostraParola() { + document.getElementById('word-container').innerText = parolaIndovinata.join(' '); +} + +// === AGGIORNA TESCHI === +function aggiornaTeschi() { + // Nascondi tutti + for (let i = 1; i <= 6; i++) { + document.getElementById(`hangman-${i}`).style.display = 'none'; + } + // Mostra teschi persi + let teschiVisibili = 6 - tentativiRimanenti; + for (let i = 1; i <= teschiVisibili; i++) { + document.getElementById(`hangman-${i}`).style.display = 'inline-block'; + } +} + +// === MOSTRA LETTERE USATE === +function mostraLettereUsate() { + let testo = lettereUsate.length > 0 ? lettereUsate.join(', ') : 'nessuna'; + document.getElementById('guessed-letters').innerText = 'Lettere usate: ' + testo; +} + +// === CREA TASTIERA === +function creaTastiera() { + const contenitore = document.getElementById('alphabet-container'); + contenitore.innerHTML = ''; + + // 3 righe tastiera + const righe = [ + ['q','w','e','r','t','y','u','i','o','p'], + ['a','s','d','f','g','h','j','k','l'], + ['z','x','c','v','b','n','m'] + ]; + + for (let riga of righe) { + let divRiga = document.createElement('div'); + divRiga.classList.add('alphabet-row'); + + for (let lettera of riga) { + let bottone = document.createElement('div'); + bottone.classList.add('alphabet-letter'); + bottone.textContent = lettera.toUpperCase(); + bottone.addEventListener('click', function() { indovinaLettera(lettera) }); + divRiga.appendChild(bottone); + } + contenitore.appendChild(divRiga); + } +} + +// === INDOVINA LETTERA === +function indovinaLettera(lettera) { + // Se già usata + if (lettereUsate.includes(lettera)) { + alert('Già provata!'); + return; + } + + lettereUsate.push(lettera); + + let indovinata = false; + + // Cerca lettera nella parola + for (let i = 0; i < parolaScelta.length; i++) { + if (parolaScelta[i] === lettera) { + parolaIndovinata[i] = lettera; + indovinata = true; + } + } + + if (!indovinata) { + tentativiRimanenti--; + } + + // Aggiorna schermo + mostraParola(); + aggiornaTeschi(); + mostraLettereUsate(); + creaTastiera(); + + // Controlla fine + controllaFine(); +} + +// === CONTROLLA FINE PARTITA === +function controllaFine() { + let parolaCompleta = parolaIndovinata.join(''); + + if (parolaCompleta === parolaScelta) { + setTimeout(() => { + alert('🎉 HAI VINTO!'); + resettaGioco(); + }, 300); + } else if (tentativiRimanenti <= 0) { + setTimeout(() => { + alert('💀 HAI PERSO! Era: ' + parolaScelta.toUpperCase()); + resettaGioco(); + }, 300); + } +} + +// === RESETTA GIOCO === +function resettaGioco() { + if (modalitaGioco === 1) { + giocoParolaCasuale(); + } else { + // Torna al menu per 2 giocatori + document.getElementById('mode-selection').style.display = 'flex'; + document.getElementById('player2-input').style.display = 'none'; + document.getElementById('game-container').style.display = 'none'; + document.querySelector('.mode-btn.active').classList.remove('active'); + } +} + +// === MOSTRA MENU INIZIALE === +function mostraSelezioneModalita() { + document.getElementById('mode-selection').style.display = 'flex'; + document.getElementById('player2-input').style.display = 'none'; + document.getElementById('game-container').style.display = 'none'; +} diff --git a/TPSI/bianchi_emanuele/gioco/style.css b/TPSI/bianchi_emanuele/gioco/style.css new file mode 100644 index 00000000..543e9f3c --- /dev/null +++ b/TPSI/bianchi_emanuele/gioco/style.css @@ -0,0 +1,225 @@ +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +body { + font-family: 'Comic Sans MS', cursive, sans-serif; + background: linear-gradient(135deg, #2c3e50, #34495e); + min-height: 100vh; + display: flex; + align-items: center; + justify-content: center; + padding: 20px; +} + +.container { + background: #1a1a1a; + background-image: + radial-gradient(circle at 25% 25%, #333 2%, transparent 2%), + radial-gradient(circle at 75% 75%, #333 2%, transparent 2%); + border: 8px solid #8B4513; + border-radius: 20px; + padding: 40px; + max-width: 700px; + box-shadow: + 0 0 50px rgba(0,0,0,0.8), + inset 0 0 50px rgba(0,0,0,0.3); +} + +/* Titolo gesso */ +h1 { + color: #f4e4bc; + font-size: 3em; + text-shadow: + 2px 2px 0 #8B4513, + 4px 4px 0 #A0522D; + text-align: center; + margin-bottom: 30px; + letter-spacing: 3px; +} + +/* Selezione modalità */ +.mode-selection { + display: flex; + gap: 20px; + justify-content: center; + margin-bottom: 30px; +} + +.mode-btn { + background: linear-gradient(145deg, #8B4513, #A0522D); + color: #f4e4bc; + border: none; + padding: 15px 30px; + font-size: 1.2em; + border-radius: 15px; + cursor: pointer; + text-shadow: 1px 1px 0 #654321; + box-shadow: + 0 4px 8px rgba(0,0,0,0.3), + inset 0 2px 4px rgba(255,255,255,0.2); + transition: all 0.3s; +} + +.mode-btn:hover { + transform: translateY(-2px); + box-shadow: 0 6px 12px rgba(0,0,0,0.4); +} + +.mode-btn.active { + background: linear-gradient(145deg, #D2691E, #CD853F); + box-shadow: 0 0 20px rgba(210,105,30,0.5); +} + +/* Input giocatore 2 */ +.input-section { + text-align: center; + margin-bottom: 30px; +} + +#custom-word { + background: #2c2c2c; + color: #f4e4bc; + border: 3px solid #8B4513; + padding: 15px; + font-size: 1.2em; + border-radius: 10px; + width: 300px; + text-align: center; + font-family: inherit; + text-shadow: none; +} + +#custom-word::placeholder { + color: #888; +} + +/* Teschi */ +#hangman-container { + display: flex; + justify-content: center; + margin-bottom: 30px; +} + +.hangman { + font-size: 3.5em; + margin: 0 8px; + display: none; + filter: drop-shadow(3px 3px 0 #654321); +} + +/* Parola */ +.word-container { + font-size: 2.5em; + font-weight: bold; + color: #f4e4bc; + text-shadow: + 3px 3px 0 #8B4513, + 6px 6px 0 #A0522D; + letter-spacing: 15px; + margin-bottom: 20px; + min-height: 80px; + display: flex; + align-items: center; + justify-content: center; +} + +/* Lettere usate */ +#guessed-letters { + color: #f4e4bc; + font-size: 1.3em; + margin-bottom: 25px; + text-shadow: 2px 2px 0 #654321; +} + +/* Tastiera */ +#alphabet-container { + display: flex; + flex-direction: column; + align-items: center; + gap: 10px; + margin-bottom: 40px; +} + +.alphabet-row { + display: flex; + gap: 8px; +} + +.alphabet-letter { + width: 50px; + height: 50px; + background: linear-gradient(145deg, #8B4513, #A0522D); + color: #f4e4bc; + border: 3px solid #D2691E; + border-radius: 12px; + display: flex; + align-items: center; + justify-content: center; + font-size: 1.4em; + font-weight: bold; + cursor: pointer; + text-shadow: 1px 1px 0 #654321; + box-shadow: + 0 4px 8px rgba(0,0,0,0.4), + inset 0 2px 4px rgba(255,255,255,0.1); + transition: all 0.2s; + user-select: none; +} + +.alphabet-letter:hover { + transform: translateY(-3px); + box-shadow: + 0 6px 12px rgba(0,0,0,0.5), + inset 0 2px 4px rgba(255,255,255,0.2); +} + +.alphabet-letter:active { + transform: translateY(0); +} + +/* Pulsanti */ +button { + background: linear-gradient(145deg, #D2691E, #CD853F); + color: #f4e4bc; + border: 3px solid #8B4513; + padding: 15px 40px; + font-size: 1.3em; + border-radius: 15px; + cursor: pointer; + font-family: inherit; + text-shadow: 2px 2px 0 #654321; + box-shadow: + 0 6px 12px rgba(0,0,0,0.4), + inset 0 2px 4px rgba(255,255,255,0.2); + transition: all 0.3s; +} + +button:hover { + transform: translateY(-2px); + box-shadow: 0 8px 16px rgba(0,0,0,0.5); +} + +button:active { + transform: translateY(0); +} + +/* Responsive */ +@media (max-width: 600px) { + .container { + padding: 20px; + margin: 10px; + } + + h1 { + font-size: 2em; + } + + .alphabet-letter { + width: 45px; + height: 45px; + font-size: 1.2em; + } +}