Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
invertir una matriz en javascript en función sin el método reverse ()

aquí está mi código:

 const reverseArr = (...args) => { let length = args.length - 1 let reversed = [] let i = 0 for (let a = length - i; i <= length; i++) { args[a] = reversed[i] } return reversed } console.log( reverseArr(3, 5, 4, 1) )

¿Cuál es el problema aquí? ¿Se trata del valor de 'i' o de la longitud?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const numbers = [1, 2, 3, 4, 5]; const reverse = (array) => { const reversed = []; for (const [index, element] of numbers.entries()) reversed[array.length - index - 1] = element; return reversed; } console.log(reverse(numbers)); // [ 5, 4, 3, 2, 1 ]

Podemos usar un bucle for ... of en las entradas de la matriz que queremos invertir y empujar el elemento a la posición deseada de la matriz.

Para calcular la posición que queremos, podemos simplemente hacer array length - index - 1 .

Esto significa que si tenemos una matriz de 5 elementos, [1, 2, 3, 4, 5] e iteramos, estaremos haciendo...

  1. 5 - 0 - 1 = 4 para el índice del primer elemento.
  2. 5 - 1 - 1 = 3 para el índice del segundo elemento.
  3. 5 - 2 - 1 = 2 para el índice del tercer elemento.
  4. 5 - 3 - 1 = 1 para el índice del cuarto ítem.
  5. 5 - 4 - 1 = 0 para el índice del quinto ítem.
about 4 years ago · Juan Pablo Isaza Report

0

function reverse(arr){ // get length var len = arr.length; // create new array var newArr = []; // loop through array for(var i = len - 1; i >= 0; i--){ // push to new array newArr.push(arr[i]); } // return new array return newArr;}

inversa ([1,2,3,4,5]);

prueba esto.

about 4 years ago · Juan Pablo Isaza Report

0

Hay varios problemas:

  1. for inicialización ocurre una vez, por lo que si let a = lemgth - i , no se actualiza más, aunque i cambie.
  2. Está reasignando valores a su propia matriz de entrada con una matriz reversed , que está vacía.
  3. Está generando la matriz de entrada (arr) .

Traté de arreglar su fragmento y también agregué un mejor enfoque:

 const reverseArr = (...args) => { let length = args.length - 1 let reversed = [] let i = 0 // update 'a' as soon as you update 'i' for(let a = length - i; i <= length; i++, a = length - i){ // update the standby array 'reveresed' reversed[i] = args[a] } // print the reversed array instead of 'args' console.log(reversed) } // Alternate approach (in-place reverse) const reverseInput = (...args) => { let left = 0, right = args.length - 1; while(left <= right) { // swap the left and right elements in array [args[left], args[right]] = [args[right], args[left]] ; left++; right--; } console.log(args); } // Approach 1 console.log(reverseArr(3, 5, 4, 1)) // Approach 2 console.log(reverseInput(3,5,4,1));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!