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.
I don't know exactly what you are trying to do, but here are few things you are doing wrong.
circumeference and diameter are defined inside the function scope and you are trying to access it outside the function, which is causing this error.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);
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.