Identify the greater of 5 Paragraphs (p)

0

I need your help as soon as possible, I have an exercise that consists of identifying the "number of characters" of 5 paragraphs and determine which is the paragraph that has the most number of letters through an Alert (); I was able to determine the number of letters in each paragraph and compare them, as well as show through the Alert (); the greater amount of letters, BUT, I can not show WHAT is the paragraph, that is, if the I, II, III, IV or V. I would appreciate your help, I attach my code.

    function identificar () {
    let parrafo = document.querySelector('#parrafo').innerHTML.length;
    alert ('El tamaño del parrafo I es de ' + parrafo + ' caracteres'); 
    let parrafo2 = document.querySelector('#parrafo2').innerHTML.length;
    alert ('El tamaño del parrafo II es de ' + parrafo2 + ' caracteres');
    let parrafo3 = document.querySelector('#parrafo3').innerHTML.length;
    alert ('El tamaño del parrafo III es de ' + parrafo3 + ' caracteres'); 
    let parrafo4 = document.querySelector('#parrafo4').innerHTML.length;
    alert ('El tamaño del parrafo IV es de ' + parrafo4 + ' caracteres');
    let parrafo5 = document.querySelector('#parrafo5').innerHTML.length;
    alert ('El tamaño del parrafo V es de ' + parrafo5 + ' caracteres');

    let comparar = [parrafo, parrafo2,parrafo3,parrafo4,parrafo5];

    let Parrafomayor = 0;
        for (i = 0; i < comparar.length; i++) {
            if (comparar[i] > Parrafomayor) {

            Parrafomayor = comparar[i];

    }
}
   alert ('El Parrafo con mayor cantidad de caracteres es el número: ' + Parrafomayor);

}

SHOWS ME THE AMOUNT OF LETTERS OF THE PARAGRAPH, but does not tell me what the Paragraph is.

    
asked by morsa 19.11.2018 в 03:57
source

2 answers

0

Welcome, walrus.

I think that your code only needs to add a variable that stores the number of the paragraph when it determines that it is the one with the most characters, a process that you already achieved with your cycle. In your example, it would be something like:

    let Parrafomayor = 0;
    let PosicionParrafomayor = 0; // aqui ira el indicador del parrafo

    for (i = 0; i < comparar.length; i++) {
        if (comparar[i] > Parrafomayor) {

        Parrafomayor = comparar[i];
        PosicionParrafomayor = i + 1; // Ya que la posición en el mundo real parte en 1, no en cero como ocurre en el array
      }
   }

And then in your output message:

alert ('El Parrafo con mayor cantidad de caracteres es el número: ' + PosicionParrafomayor);

    
answered by 19.11.2018 в 05:06
0

you can do it like this:

//seleccionamos los elementos de la etiqueta p que su id empieze con parrafo
parrafos = document.querySelectorAll("p[id^='parrafo']");

   //creamos un arreglo para el mayor y el parrafo especifico!
   mayor = [0,0];
  //hacemos un loops
for (var i = parrafos.length - 1; i >= 0; i--) {
// si es mayor al anterior agrega el parrafo a la posicion 1 y en la 0 la cantidad de letras
  if (parrafos[i].innerHTML.length > mayor[0]) {
      mayor[0] = parrafos[i].innerHTML.length;
      mayor[1] = parrafos[i]; 
  }
}
alert('el parrafo mas lago es el ${mayor[1].id} con ${mayor[1].innerHTML.length} caracteres');
<p id="parrafo1">hola</p>
<p id="parrafo2">hola1</p>
<p id="parrafo3">hola22</p>
<p id="parrafo4">hola333</p>
<p id="parrafo5">hola4444</p>
<p id="parrafo6">hola55555</p>
<p id="parrafo7">hola666666</p>

>

    
answered by 19.11.2018 в 05:08