Advertisement
iEmanuele

saluto.js

Nov 17th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Assegno il valore del prompt ( o user input )
  2. var nominativo = prompt('Inserire nominativo');
  3.  
  4. //Convalida
  5. //Se il valore NON È vuoto E NON È nullo
  6. if(nominativo != '' && nominativo != null){
  7.     //Determino il sesso dell'utente
  8.     var gender = getGender(nominativo);
  9.     //Determino il saluto
  10.     var string = gender == 'female' ? 'Benvenuta ' : 'Benvenuto ';
  11.     //Output
  12.     alert(string + nominativo);
  13. //Altrimenti
  14. }else{
  15.     alert('Devi inserire il tuo nominativo');
  16. }
  17.  
  18. /**
  19.  * Determina il sesso dell'utente valutando il nominativo
  20.  * @param  {string} nominativo [nominativo dell'utente: nome OPPURE nome + cognome]
  21.  * @return {string} gender     [sesso]
  22.  */
  23. function getGender(nominativo){
  24.     var array = nominativo.split(' ');
  25.     if( array > 1 ){
  26.         //Assumendo che la prima parola sia il nome e che l'utente non abbia doppio nome...
  27.         var start = array[0].length;
  28.         var end = array[0].length -1;
  29.         if( array[0].substring( start, end) == 'a' ){
  30.             return 'female';
  31.         }else{
  32.             return 'male';
  33.         }
  34.     }else{
  35.         var start = nominativo.length;
  36.         var end   = nominativo.length -1;
  37.         if( nominativo.substring( start, end) == 'a' ){
  38.             return 'female';
  39.         }else{
  40.             return 'male';
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement