Advertisement
fcamuso

Javascript ES13, video 20

Feb 6th, 2022
1,198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //** QUESTA FUNZIONE ANDREBBE SALVATA IN UN FILE A PARTE INCORPORANDOLA CON <script src="nome_file.js"> </script>
  2. //come mostrato nel sorgente html che segue la funzione
  3. function leggiNumero(messaggio, richiediIntero=false)
  4. {
  5.   let numeroStr = prompt(messaggio);  
  6.  
  7.   //da separatore italiano a anglosassone
  8.   if (numeroStr!=="" && numeroStr!=null)
  9.     numeroStr = numeroStr.replace(",", "."); //da 3,67 -> 3.67
  10.  
  11.   let valore = parseFloat(numeroStr);
  12.  
  13.   if (numeroStr === null)
  14.   { alert("Hai premuto ESC o fatto clic su CANCEL ..."); }
  15.   else
  16.   {
  17.     if (numeroStr==="")
  18.     { alert("Hai confermato senza aver inserito nulla");}
  19.     else
  20.     {
  21.       if (isNaN(valore) || valore.toString().length!==numeroStr.length)
  22.       { alert("Formato non numerico");}
  23.       else
  24.       {      
  25.         //deve essere un intero?
  26.         if (richiediIntero  && numeroStr.indexOf(".") > -1)  
  27.           alert("Non è un numero intero");
  28.         else
  29.           return valore;
  30.       }
  31.     }
  32.   }
  33.  
  34.   return NaN;
  35. }
  36.  
  37. //PAGINA HTML CHE INCORPORA LO SCRIPT PRECEDENTE
  38. <!--  Inserite le misure dei lati di 2 rettangoli dire
  39.       quale dei due ha la superficie maggiore -->
  40. <!DOCTYPE html>
  41. <html lang="en">
  42. <head>
  43.   <meta charset="UTF-8">
  44.   <title>Esercizi IF - 1</title>
  45. </head>
  46. <body>
  47.   <script src="funzioniUtili.js"></script>
  48.  
  49.   <script>
  50.  
  51.     let base_r1 = leggiNumero("Inserire base primo rettangolo");
  52.  
  53.     let h_r1=0, base_r2=0, h_r2=0;
  54.     let area_r1=0, area_r2=0;
  55.  
  56.     if (!isNaN(base_r1))
  57.     {
  58.       h_r1 = leggiNumero("Inserire altezza primo rettangolo");
  59.       if (!isNaN(h_r1))
  60.       {
  61.         base_r2 = leggiNumero("Inserire base secondo rettangolo");
  62.         if (!isNaN(base_r2))
  63.         {
  64.           h_r2 = leggiNumero("Inserire altezza secondo rettangolo");        
  65.           if (!isNaN(h_r2))
  66.           {
  67.             area_r1=base_r1*h_r1;
  68.             area_r2=base_r2*h_r2;
  69.  
  70.             if (area_r1===area_r2)
  71.             { alert("Superfici equivalenti"); }
  72.             else
  73.             {
  74.               if (area_r1>area_r2)
  75.               { alert("Il primo rettangolo ha l'area maggiore"); }
  76.               else
  77.               { alert("Il secondo rettangolo ha l'area maggiore"); }
  78.             }
  79.           }
  80.         }
  81.       }
  82.     }
  83.   </script>
  84. </body>
  85. </html>
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement