Advertisement
fcamuso

Javascript Lezione 22

Feb 18th, 2022
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <html>
  3. <head>
  4.     <title>Espressioni condizionali</title>
  5.    
  6.     <style>
  7.      table{border: 1px dashed black;}
  8.     </style>
  9. </head>
  10.  
  11. <body>
  12.  
  13. <form id="form" action="">
  14.   <table>
  15.       <tr>
  16.          <td>Titolo di Studio</td>
  17.          <td> <input type="radio" name="titolo" value="diploma" checked> Diploma 100</td>
  18.          <td> <input type="radio" name="titolo" value="laurea"> Laurea 300</td>
  19.      </tr>
  20.      <tr>
  21.          <td>Esperienza lavorativa</td>
  22.          <td> <input type="radio" name="esperienza" value="meno" checked> Meno di 5 anni 200</td>
  23.          <td> <input type="radio" name="esperienza" value="piu"> Più di 5 anni 400</td>
  24.      </tr>
  25.  
  26.      <tr>
  27.          <td>Linguaggio MAX exp</td>
  28.          <td> <input type="radio" name="linguaggio" value="C"> C 100</td>
  29.          <td> <input type="radio" name="linguaggio" value="C++" > C++ 200</td>
  30.          <td> <input type="radio" name="linguaggio" value="PHP" > PHP 250</td>
  31.          <td> <input type="radio" name="linguaggio" value="Javascript" > Javascript 250</td>
  32.          <td> <input type="radio" name="linguaggio" value="Java" > Java 150</td>
  33.      </tr>
  34.  
  35.    </table>
  36.    <br>
  37.    <input type="button" id="btn_stima" value="STIMA RETRIBUZIONE">
  38.    <input type="text" id="retribuzione1">
  39.    <input type="text" id="retribuzione2">
  40.    <input type="text" id="retribuzione3">
  41.    <input type="text" id="retribuzione4">
  42.    <input type="text" id="retribuzione5">
  43. </form>
  44.  
  45.  <script>
  46.    function stima_retribuzione()
  47.     {
  48.        let stipendio = 1200; //stipendio base
  49.  
  50.        let titolo = document.querySelector("[name='titolo']:checked").value;    
  51.        let esperienza = document.querySelector("[name='esperienza']:checked").value;
  52.  
  53.        if (titolo==="diploma")
  54.        {stipendio = stipendio + 100;} //forma compatta: stipendio+=100
  55.        else
  56.        {stipendio = stipendio + 300;}
  57.  
  58.        if (esperienza==="meno")
  59.        {stipendio = stipendio + 200;}
  60.        else
  61.        {stipendio = stipendio + 400;}
  62.        document.querySelector("#retribuzione4").value=stipendio;
  63.  
  64.        stipendio = 1200; //reset per nuovo calcolo
  65.  
  66.        //RIFACCIAMO IL CALCOLO CON ESPRESSIONI CONDIZIONALI
  67.  
  68.        // forma piú tranquilla in due passaggi
  69.        //  stipendio = stipendio + (titolo==="diploma" ? 100 : 300);
  70.        //  stipendio = stipendio + (esperienza==="meno" ? 200 : 400);
  71.  
  72.        //forma piú compatta usando due espressioni condizionali nella stessa espressione
  73.        stipendio = stipendio +
  74.           (titolo==="diploma" ? 100 : 300) +
  75.           (esperienza==="meno" ? 200 : 400);
  76.        
  77.        //esageriamo  (ed evitiamo!)...
  78.        //  stipendio = stipendio + (titolo==="diploma" ? 100 + (esperienza==="meno" ? 200 : 400) : 300 + (esperienza==="meno" ? 200 : 400) );
  79.        document.querySelector("#retribuzione5").value=stipendio;
  80.  
  81.    
  82.       let form = document.querySelector("#form");
  83.       let linguaggio = form.elements.linguaggio.value;
  84.       //let linguaggio = document.querySelector("[name='linguaggio']:checked").value;
  85.  
  86.        //linguaggio, con if else
  87.        if (linguaggio==="C")
  88.        {stipendio+=100;}
  89.        else
  90.        {
  91.         if (linguaggio==="C++")
  92.         { stipendio+=200;}
  93.         else
  94.         {
  95.           if (linguaggio==="PHP" || linguaggio==="Javascript")
  96.           {stipendio+=250;}
  97.           else
  98.           {
  99.             if (linguaggio==="Java")
  100.             {stipendio+=150;}
  101.             else
  102.             {stipendio-=50;}
  103.           }
  104.         }
  105.        }
  106.        document.querySelector("#retribuzione3").value=stipendio;
  107.  
  108.        stipendio=1200 +
  109.           (titolo==="diploma" ? 100 : 300) +
  110.           (esperienza==="meno" ? 200 : 400);    
  111.  
  112.        //linguaggio = Math.random() > 0.5 ? "C" : 4; //case anche di tipo misto
  113.  
  114.        switch (linguaggio)  
  115.        {
  116.            case 4:
  117.              stipendio+=100;
  118.            break;
  119.  
  120.            case "C":
  121.              stipendio+=100;
  122.            break;
  123.  
  124.            case "C++":
  125.              stipendio+=200;
  126.            break;
  127.  
  128.            case "PHP":
  129.            case "Javascript":
  130.              stipendio+=250;
  131.            break;
  132.  
  133.            case "Java":
  134.              stipendio+=150;
  135.            break;
  136.  
  137.            default:
  138.              stipendio-=50;
  139.            break;
  140.         }
  141.  
  142.         document.querySelector("#retribuzione2").value=stipendio;
  143.  
  144.         let x = 3.140001;
  145.         switch (x)
  146.         {
  147.           case 3.14:
  148.           {
  149.             let y=12;
  150.             alert("pi-greco");
  151.           }
  152.           break;
  153.  
  154.           case 3.140001:
  155.           {
  156.             let y=65;
  157.             alert("QUASI pi-greco");
  158.           }
  159.           break;
  160.         }
  161.  
  162.         x=0;
  163.         let c=0;
  164.         switch (c)
  165.         {
  166.           case 0:
  167.             x+=100;
  168.           case 1:
  169.             x+=1000;
  170.           case 3:
  171.             x+=10000;
  172.           case 4:
  173.             x+=100000;
  174.           break;
  175.  
  176.           default:
  177.             x=1000000;
  178.           break;
  179.         }
  180.         alert(x);
  181.  
  182.        
  183.     }
  184.  
  185.  </script>
  186.  
  187.  <script>
  188.   document.querySelector("#btn_stima").addEventListener("click", stima_retribuzione);
  189.    
  190.        
  191.  </script>
  192.  
  193. </body>
  194. </html>
  195.  
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement