Actualmente uso este bit de javascript dentro de Adobe After Effects para imprimir una lista personalizable de números
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")que (en este caso) generaría
0 20 40 60 80Sería genial si también pudiera generar la lista al revés
80 60 40 20 0pero como no soy programador, no tengo idea de cómo hacer esto. ¿Podría alguien ayudarme con esto?
En lugar de comenzar desde el primer número ( x ) y agregar y para cada iteración, puede comenzar desde el último número y restar y para cada iteración.
El último número sería el número de veces que y se suma a x . Eso da la fórmula (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);Creo que no hay necesidad de optimización aquí, ya que el operador no tiene experiencia en programación, una solución simple es lo suficientemente buena, que sería Array.reverse() .
Consulte los documentos de MDN para conocer el uso correcto.
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);Otra posible solución : simplemente complete su matriz desde la parte posterior. Entonces no hay necesidad de dar marcha atrás.
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);