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

332
Vistas
Java Script Function changes parameter variables globally, how to avoid that?

rotr() removes the last array value and places it to array-index 0 (to the the beginning). The method does that for n times. Its rotating the array x by n.

My problem is the first XORXOR parameter is w[sh1] rotatet by 7. Okay! Thats good.

The second XORXOR parameter is rotatet by 7 + 18 and the last parameter is rotatet by 7+18+3. Thats not how i want it...

The first parameter of rotr should and must always be the same array. Why is the function changing the array globally. Its not modifying the parameter and returns a copy of that modification? With return? Always used Python... then one time a little bit JS and its completely getting on my nerves... can someone help me and explain it to me?? my whole algorithm is a failure because of that *****

const s0 = XORXOR(rotr(w[sh1], 7), rotr(w[sh1], 18), shr(w[sh1], 3))

function rotr(x, n) {
    for(let c = 0; c<n; c++) {
        x.unshift(x.pop());
    }
    return x
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use the spread operator (...)

const s0 = [1, 2, 3, 4];

function rotr(x, n) {
  const copy = [...x];
  for (let c = 0; c < n; c++)
    copy.unshift(copy.pop());

  console.log(`original array: ${x} and changed array: ${copy}`)
  return copy
}

rotr(s0, 2)

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