Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
reversing an array in javascript in function without method reverse()

here is my code:

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) )

What's the problem here? Does it about value of 'i' or lenght?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

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;}

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

try this.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are multiple issues :

  1. for initialisation happens once, so if you do let a = lemgth - i, it doesn't update a any further, even though i changes.
  2. You're reassigning values to your input array itself with reversed array, which is empty itself.
  3. You're output-ing the input array (arr).

I tried to fix your snippet, and also adding better approach:

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 Denunciar

0

simply...

const reverseArr = (...args) => Array.from({length:args.length},_=>args.pop())
 
console.log( reverseArr(3, 5, 4, 1) )

if you want to use an array as argument and let it untouched:

const reverseArr = arr => Array.from({length:arr.length},(_,i)=>arr[(arr.length-++i)])


let arrOrigin = [3,5,4,1]
let arrResult = reverseArr( arrOrigin )

console.log( arrOrigin )
console.log( arrResult )
.as-console-wrapper {max-height: 100%!important;top:0 }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda