Advertisement
piffy

3.2.10 Esercizio 6

Aug 29th, 2021
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.98 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="it">
  3. <head>
  4.   <meta charset="UTF-8">
  5.   <title>3.2.10 Es 6</title>
  6. </head>
  7. <body>
  8. <table style="border: 1px solid lightgray">
  9.   <tr><th>Nome</th><th>Età</th></tr>
  10.  
  11. <script>
  12.  
  13.   function confrontaStringhe( a, b ) {
  14.     if ( a.nome < b.nome ){return -1;}
  15.    if ( a.nome > b.nome ){return 1;}
  16.     return 0;
  17.   }
  18.  
  19.  
  20.   let arr=[];let dato; let media=0;
  21.   do {
  22.     dato={};
  23.     dato.nome=prompt("inserisci nome");
  24.     let e=prompt("inserisci età");
  25.     dato.eta=parseInt(e);
  26.     //document.getElementById("demo").innerHTML ="Input: " + dato.eta ;
  27.     arr.push(dato);
  28.   } while (dato.nome!="");
  29.   arr.pop();
  30.   console.log(arr);
  31.   arr.sort(confrontaStringhe);
  32.   console.log(arr);
  33.  
  34.   for (let i = 0; i < arr.length; i++) {
  35.    document.write("<tr></tr><td>"+arr[i].nome+"</td><td>"+arr[i].eta+"</td></tr>");
  36.     media += arr[i].eta;
  37.   }
  38.   media=media/arr.length;
  39.   document.write("</table><p>Media: "+media+"</p>");
  40.  
  41. </script>
  42. </body>
  43. </html>
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement