Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
How reverse the order of an array

I currently use this bit of javascript inside Adobe After Effects to print a customizable list of numbers

x = 0 //starting point (the first number in the list);
y= 20 //increments (the size of the steps);
z= 5 //the number of steps;

Array.from(new Array(z)).map((_, i) => i * y + x).join("\n")

which (in this case) would output

0
20
40
60
80

It would be really cool if I could also generate the list in reverse

80
60
40
20
0 

but because I'm not a programmer I have no idea how to do this. Could someone help me with this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Instead of starting from the first number (x) and adding y for each iteration, you can start from the last number and subtract y for every iteration.

The last number would be the number of times y is added added to x. That gives the formulat (z - 1) * y) + x

x = 0 //starting point (the first number in the list);
y= 20 //increments (the size of the steps);
z= 5 //the number of steps;

const result = Array.from(new Array(z))
  .map((_, i) => (((z - 1) * y) + x) - (i * y)).join("\n");
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think there is no need to optimisation here, as the op has no programming experience, a simple solution is well enough, which would be the Array.reverse().

See the the MDN docs for correct usage.

const x = 0 //starting point (the first number in the list);
const y= 20 //increments (the size of the steps);
const z= 5 //the number of steps;

const array = Array.from({length: z}, ((_, i) => i * y + x)).reverse().join("\n");

console.log(array);

Another possible solution: simply populate your array from the back. Then no need to reverse.

 const x = 0 //starting point (the first number in the list);
    const y= 20 //increments (the size of the steps);
    const z= 5 //the number of steps;

    const array = Array.from({length: z}, ((_, i) => (z * y - y) - i * y)).join("\n");

    console.log(array);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda