Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!