Ver Mensaje Individual
  #9 (permalink)  
Antiguo 24/12/2014, 11:47
Avatar de Alexis88
Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 6 meses
Puntos: 977
Respuesta: Contar palabras repetidas

Wizerol: ¿Por qué colocaste el código de la invocación de la función dentro de la función? No debiste de hacerlo. Por otra parte, al darle clic al <textarea>, no mostrarás nada ya que la función retorna un objeto, mas no lo imprime. Eso ya te toca hacerlo o bien podrías modificar la función:

Código HTML:
Ver original
  1. <textarea onclick = "array_count_values(this.value)">Este año fue muy bueno, excepto por dos acontecimientos muy dolorosos: la muerte de mi padre y la desaparición de mi compañera y amiga: mi gatita Ginny</textarea>

Código Javascript:
Ver original
  1. function array_count_values(texto) {
  2.  
  3.   var array = texto.split(/[^a-zñáéíóúÁÉÍÓÚÑ\d]+/gi);
  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.   var valores = [];
  44.  
  45.   for (var i in tmp_arr) valores.push("La palabra '" + i + "' se repite " + tmp_arr[i] + " vez/veces");
  46.   alert(valores.join("\n") + "\n\nHay " + (texto.match(/\s+/g).length) + " espacios" + "\nHay " + array.length + " palabras");
  47. }

DEMO

Saludos
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand

Última edición por Alexis88; 24/12/2014 a las 11:57 Razón: Mejora