Ver Mensaje Individual
  #12 (permalink)  
Antiguo 24/12/2014, 13:14
Avatar de Wizerol
Wizerol
 
Fecha de Ingreso: octubre-2014
Mensajes: 35
Antigüedad: 10 años, 3 meses
Puntos: 0
Respuesta: Contar palabras repetidas

No entiendo lo que hago mal, no funciona pero no encuentro otra manera :S

Código HTML:
Ver original
  1.  
  2.  
  3.  
  4. </head>
  5.  
  6. <div align='center'>
  7. <textarea id="texto" cols=70 rows=5></textarea><br><br>
  8. <input href="#" class="myButton" type="button" value="Start" onClick="array_count_values(texto);">
  9. </div>
  10.  
  11. <script type="text/javascript" src="nump.js"></script>
  12. </body>
  13.  
  14. </html>

Código Javascript:
Ver original
  1. function array_count_values(texto) {
  2.  
  3.   var array = texto.value.trim().replace(sim, ' ').split(' ').length;
  4.  
  5.   var tmp_arr = {};
  6.     key = '';
  7.     t = '';
  8.  
  9.   var __getType = function(obj) {
  10.     var t = typeof obj;
  11.     t = t.toLowerCase();
  12.     if (t === 'object') {
  13.       t = 'array';
  14.     }
  15.     return t;
  16.   };
  17.  
  18.   var __countValue = function(value) {
  19.     switch (typeof value) {
  20.       case 'number':
  21.         if (Math.floor(value) !== value) {
  22.           return;
  23.         }
  24.  
  25.       case 'string':
  26.         if (value in this && this.hasOwnProperty(value)) {
  27.           ++this[value];
  28.         } else {
  29.           this[value] = 1;
  30.         }
  31.     }
  32.   };
  33.  
  34.   t = __getType(array);
  35.   if (t === 'array') {
  36.     for (key in array) {
  37.       if (array.hasOwnProperty(key)) {
  38.         __countValue.call(tmp_arr, array[key]);
  39.       }
  40.     }
  41.   }
  42.    
  43.     console.log(texto.match(/\s+/g));
  44.  
  45.   var valores = [];
  46.  
  47.   for (var i in tmp_arr) valores.push("La palabra '" + i + "' se repite " + tmp_arr[i] + " vez/veces");
  48.     alert(valores.join("\n") + "\n\nHay " + (texto.match(/\s+/g).length) + " espacios" + "\nHay " + array.length + " palabras");
  49. }