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

148
Visualizações
why I got reference error and What is wrong with my code?

let radius = [3, 2, 1, 4];

function area(radius) {
    let tArea = [];
    let circumference = [];
    let diameter = [];

    for (let i = 0; i < radius.length; i++) {
        tArea.push(Math.PI * radius[i] * radius[i]);
        circumference.push(2 * Math.PI * radius[i]);
        diameter.push(2 * radius[i]);
    }
    return tArea;
    return circumference;
    return diameter;
}

console.log(area(radius));
console.log(area(circumference));
console.log(area(diameter));

hello experts, I'm not getting, why this code does not work. I'm getting a reference error. please explain if possible and teach me how to debug the code so I wont be disturbing you guys for small errors. Thank you.

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

0

I don't know exactly what you are trying to do, but here are few things you are doing wrong.

  1. circumeference and diameter are defined inside the function scope and you are trying to access it outside the function, which is causing this error.
  2. You are trying to return multiple values from a function using multiple return statements which is not possible since the first return will end the function. For your purpose you can simply return an object from the function, containing your desired values.

See the code below,

let radius = [3, 2, 1, 4];

function area(radius) {
    let tArea = [];
    let circumference = [];
    let diameter = [];

    for (let i = 0; i < radius.length; i++) {
        tArea.push(Math.PI * radius[i] * radius[i]);
        circumference.push(2 * Math.PI * radius[i]);
        diameter.push(2 * radius[i]);
    }
    return {area: tArea, circumference, diameter};
}

const {area: calculatedArea, circumference, diameter} = area(radius);

console.log(calculatedArea);
console.log(circumference);
console.log(diameter);

about 4 years ago · Juan Pablo Isaza Relatório

0

Because you can return only a single value from a function while you are trying to return 3 . Declare these 3 arrays globally and you won't need the return statements.

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