School-HTML/3AIN/Esercizi TPS Vichi per 2025.../esercizi.js

152 lines
4.8 KiB
JavaScript

function esercizio1() {
const text = document.getElementById("textInput").value;
const words = text
.toLowerCase()
.replace(/[^\w\sàèéìòù]/g, '')
.split(/\s+/)
.filter(word => word.length > 0);
if (words.length === 0) {
document.getElementById("longestWordResult").textContent = "Nessuna parola trovata.";
document.getElementById("wordOccurrences").textContent = "";
return;
}
let longestWord = words.reduce((a, b) => (b.length > a.length ? b : a));
const occurrences = {};
for (let word of words) {
occurrences[word] = (occurrences[word] || 0) + 1;
}
document.getElementById("longestWordResult").textContent =
"🔍 Parola più lunga: " + longestWord;
let occHTML = "<h3>📊 Occorrenze delle parole:</h3><ul>";
for (let word in occurrences) {
occHTML += `<li><strong>${word}</strong>: ${occurrences[word]}</li>`;
}
occHTML += "</ul>";
document.getElementById("wordOccurrences").innerHTML = occHTML;
}
function esercizio2() {
const input = document.getElementById('numberInput').value;
const numeri = input
.split(';')
.map(n => parseFloat(n.trim()))
.filter(n => !isNaN(n));
const output = document.getElementById('output');
if (numeri.length === 0) {
output.textContent = "⚠️ Nessun numero valido inserito.";
return;
}
numeri.sort((a, b) => a - b);
output.textContent = "✅ Numeri ordinati: " + numeri.join(', ');
}
function esercizio3() {
const testo = document.getElementById("inputText").value;
const parole = testo.trim().split(/\s+/);
let acronimo = "";
for (let parola of parole) {
if (parola.length > 0) {
acronimo += parola[0].toUpperCase();
}
}
document.getElementById("acronym").textContent = acronimo ? "Acronimo: " + acronimo : "";
}
function isPalindroma(frase) {
const pulita = frase.toLowerCase().replace(/[^a-z0-9]/gi, '');
const invertita = pulita.split('').reverse().join('');
return pulita === invertita;
}
function esercizioOmaggio() {
const input = document.getElementById('inputFrase').value.trim();
const risultatoDiv = document.getElementById('risultato');
if (!input) {
risultatoDiv.textContent = "⚠️ Inserisci una frase!";
risultatoDiv.className = 'negativo';
return;
}
if (isPalindroma(input)) {
risultatoDiv.textContent = "✅ La frase è palindroma!";
risultatoDiv.className = 'positivo';
} else {
risultatoDiv.textContent = "❌ La frase NON è palindroma.";
risultatoDiv.className = 'negativo';
}
}
//Funzioni per prevenire la copia
document.oncontextmenu = document.body.oncontextmenu = function() {
document.write(`
Right click is not allowed on this site! Reload page!
`);
return false;
};
function devToolsOpened(e){
e.preventDefault();
}
window.addEventListener('keydown', function(e) {
if (
e.metaKey == true && e.altKey == true && e.keyCode == 73 ||
e.metaKey == true && e.altKey == true && e.keyCode == 74 ||
e.metaKey == true && e.altKey == true && e.keyCode == 67 ||
e.metaKey == true && e.shiftKey == true && e.keyCode == 67 ||
e.ctrlKey == true && e.shiftKey == true && e.keyCode == 73 ||
e.ctrlKey == true && e.shiftKey == true && e.keyCode == 74 ||
e.ctrlKey == true && e.shiftKey == true && e.keyCode == 67 ||
e.keyCode == 123 ||
e.metaKey == true && e.altKey == true && e.keyCode == 85 ||
e.ctrlKey == true && e.keyCode == 85 ||
e.ctrlKey == true && e.keyCode == 80
){
devToolsOpened(e);
}
});
const devtools = {
isOpen: false,
orientation: undefined,
};
const threshold = 170;
const emitEvent = (isOpen, orientation) => {
globalThis.dispatchEvent(new globalThis.CustomEvent('devtoolschange', {
detail: {
isOpen,
orientation,
},
}));
};
const main = ({emitEvents = true} = {}) => {
const widthThreshold = globalThis.outerWidth - globalThis.innerWidth > threshold;
const heightThreshold = globalThis.outerHeight - globalThis.innerHeight > threshold;
const orientation = widthThreshold ? 'vertical' : 'horizontal';
if (
!(heightThreshold && widthThreshold)
&& ((globalThis.Firebug && globalThis.Firebug.chrome && globalThis.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)
) {
if ((!devtools.isOpen || devtools.orientation !== orientation) && emitEvents) {
emitEvent(true, orientation);
}
devtools.isOpen = true;
devtools.orientation = orientation;
} else {
if (devtools.isOpen && emitEvents) {
emitEvent(false, undefined);
}
devtools.isOpen = false;
devtools.orientation = undefined;
}
};
setInterval(main, 500);
window.addEventListener('devtoolschange', event => {
document.write(`
Please close DevTools, then reload the page!
`);
});