Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //** QUESTA FUNZIONE ANDREBBE SALVATA IN UN FILE A PARTE INCORPORANDOLA CON <script src="nome_file.js"> </script>
- //come mostrato nel sorgente html che segue la funzione
- function leggiNumero(messaggio, richiediIntero=false)
- {
- let numeroStr = prompt(messaggio);
- //da separatore italiano a anglosassone
- if (numeroStr!=="" && numeroStr!=null)
- numeroStr = numeroStr.replace(",", "."); //da 3,67 -> 3.67
- let valore = parseFloat(numeroStr);
- if (numeroStr === null)
- { alert("Hai premuto ESC o fatto clic su CANCEL ..."); }
- else
- {
- if (numeroStr==="")
- { alert("Hai confermato senza aver inserito nulla");}
- else
- {
- if (isNaN(valore) || valore.toString().length!==numeroStr.length)
- { alert("Formato non numerico");}
- else
- {
- //deve essere un intero?
- if (richiediIntero && numeroStr.indexOf(".") > -1)
- alert("Non è un numero intero");
- else
- return valore;
- }
- }
- }
- return NaN;
- }
- //PAGINA HTML CHE INCORPORA LO SCRIPT PRECEDENTE
- <!-- Inserite le misure dei lati di 2 rettangoli dire
- quale dei due ha la superficie maggiore -->
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Esercizi IF - 1</title>
- </head>
- <body>
- <script src="funzioniUtili.js"></script>
- <script>
- let base_r1 = leggiNumero("Inserire base primo rettangolo");
- let h_r1=0, base_r2=0, h_r2=0;
- let area_r1=0, area_r2=0;
- if (!isNaN(base_r1))
- {
- h_r1 = leggiNumero("Inserire altezza primo rettangolo");
- if (!isNaN(h_r1))
- {
- base_r2 = leggiNumero("Inserire base secondo rettangolo");
- if (!isNaN(base_r2))
- {
- h_r2 = leggiNumero("Inserire altezza secondo rettangolo");
- if (!isNaN(h_r2))
- {
- area_r1=base_r1*h_r1;
- area_r2=base_r2*h_r2;
- if (area_r1===area_r2)
- { alert("Superfici equivalenti"); }
- else
- {
- if (area_r1>area_r2)
- { alert("Il primo rettangolo ha l'area maggiore"); }
- else
- { alert("Il secondo rettangolo ha l'area maggiore"); }
- }
- }
- }
- }
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement