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

90
Visualizações
undefined output in while loop w/array using Nth root equation javascript

I'm trying to write this code for an after effects expression that will move an object a calculated x distance over n number of frames. The movement over each frame is parabolic rather than linear, and so I'm using the nth root code to determine how much the object should move over each frame. I'm putting each of the nth roots into an array to access later when setting the positions for each move.

I'm still learning javascript mainly for AE, so please bear with me if there are things in here I don't fully understand. I think I understand, which is why I'm not sure I'm getting the undefined output for certain n values. Here's the code:

//get Nth root
function nthroot(x, n) {
  ng = n % 2;
  if ((ng == 1) || x < 0)
    x = -x;
  var r = Math.pow(x, 1 / n);
  n = Math.pow(r, n);

  if (Math.abs(x - n) < 1 && (x > 0 === n > 0))
    return ng ? -r : r;
}

distance=1515; //will be determined by another portion of the AE expression
frames=6; //will be set by expression control in AE
const myArray = [];
let i = 1;
while (i <= 6) {
  myArray.push(nthroot(distance,i++));

}

console.log(myArray);
document.getElementById("demo2").innerHTML = myArray

I put it into a fiddle here. What am I doing wrong? Thanks in advance for any help!

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

0

Your "nthroot" function doesn't return values consisistently: if you look at the end of your function you can find this part:

if (Math.abs(x - n) < 1 && (x > 0 === n > 0))
   return ng ? -r : r;

if you notice, you are returning a value only if the condition in the if statement is fullfilled, otherwise you are not returning any value. In JavaScript, a function that doesn't return any value returns "undefined" instead.

In other words, when

Math.abs(x - n) < 1 && (x > 0 === n > 0)

is true, you are returning either "r" or "-r", but when the condition is false you aren't returning any value, so the function returns "undefined".

You have to handle the false case.

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