The split ("") method of an array returns the messy array

0

I want to save the value of a text area in an array and I am doing it with a let array = texto.split(" ") , but when doing console.log(array) it returns it messy. What can I do?

Here is the code:

let t_Area = document.querySelector("#texto") // Obtengo el tag textArea
let texto, array, letras, palabras, firstWord, lastWord, arrInverso, orden, ordenInverso

function leer() { // Función ejecutada al clickear el botón 'Enviar'
texto = t_Area.value // Obtengo el valor del textArea en el momento del click
array = texto.split(" ") // Separo el valor del textArea por espacios, me un array con las palabras
console.log(texto)
console.log(array)


letras = 0
palabras = array.length // El numero de palabras en el texto
firstWord = array[0] // Se obtiene la primera palabra
lastWord = array[array.length - 1] // Se obtiene la ultima palabra
arrInverso = array.reverse()
orden = array.sort()
ordenInverso = orden.reverse()

for(let i = 0; i < array.length; i ++) { // El for se ejecuta según el número de palabras
    letras+= Number(array[i].length) // Calcula el total de letras, no cuenta los espacios
}

console.log("Hay " + letras + " letras") // Muestra el total de letras, no cuenta los espacios
console.log("Hay " + palabras + " palabras") // Muestra el total de palabras, no cuenta los espacios
console.log("La primera palabra es: " + firstWord) // Muestra la primera palabra del texto
console.log("La ultima palabra es: " + lastWord) // Muestra la ultima palabra del texto
console.log("El texto a la inversa es: " + arrInverso) // Muestra el texto inverso
console.log("El texto ordenado de la A a la Z es: " + orden) // Muestra el texto ordenado de la A a la Z
console.log("El texto ordenado de la Z a la A es: " + ordenInverso) // Muestra el texto ordenado de la Z a la A
    
asked by darioxlz 19.09.2018 в 01:13
source

1 answer

2

The array appears correctly as soon as you make split .

I think the problems you have happen afterwards:

  • arrInverso = array.reverse() when executing this, you are sorting to form the array array and arrInverso becomes only a reference of the variable array .
  • orden = array.sort() to execute this, you are sorting ascending the array array , arrInverso and orden becomes a reference of array .
  • ordenInverso = orden.reverse() when executing this, be sorting down the array array , arrInverso and orden and ordenInverso becomes a reference of orden which is a reference of array .
  • Check and try the following code to validate that the arrangements are the same when doing it in the same way as your code does.

    $(document).ready(function() {
    
     $('#validar').click(function() {
      let arr = $('#text').val().split(' ');
      console.log('Array normal', arr);
      
      var ordenInverso = arr.reverse();
      console.log(arr, ordenInverso);
      
      var orden = ordenInverso.sort();
      console.log(arr, ordenInverso, orden);
      
     });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea id="text" style="width: 100%;">hola una frase de ejemplo</textarea>
    <button type="button" id="validar">Validar</button>

    To be able to count on variables that contain different arrangements, you must copy the arrangement, using any of the forms presented in the code below.

        $(document).ready(function() {
    
         $('#validar').click(function() {
          let arr = $('#text').val().split(' ');
          console.log('Array normal', arr);
          
          const copy1 = arr.slice();
          const copy2 = [].concat(arr);
          const copy3 = Object.values(arr);
          const copy4 = [...arr];
          const copy5 = Array.of(...arr);
          const copy6 = JSON.parse(JSON.stringify(arr));
          const copy7 = arr.map(i => i);
          const copy8 = Object.assign([],arr);
          
          copy1.reverse();
          console.log(arr, copy1);
          
          copy2.sort();
          console.log(arr, copy1, copy2);
          
         });
    
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <textarea id="text" style="width: 100%;">hola una frase de ejemplo</textarea>
        <button type="button" id="validar">Validar</button>
        
    answered by 19.09.2018 / 01:38
    source