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

149
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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