I have 2 JavaScript functions that are similar but one of them has a hardcoded variable while the other function's variable is to be defined when called upon. Sorry if what i am talking doesn't make sense but here is the code so you can understand it more easily:
function calculateCircumference()
{
var radius = 3;
var circumference = Math.PI * 2 * radius;
console.log("The circumference is " + circumference);
}
function calculateArea()
{
var radius = 3;
var area = Math.PI * radius * radius;
console.log("The area is " + area);
}
function calculateCircumference(radius)
{
var circumference = Math.PI * 2*radius;
console.log("The circumference is " + circumference);
}
function calculateArea(radius)
{
var area = Math.PI * radius*radius;
console.log("The area is " + area);
}
calculateCircumference();
calculateArea();
calculateCircumference(5);
calculateArea(9);
Output:
The circumference is NaN
The area is NaN
The circumference is 31.41592653589793
The area is 254.46900494077323
I understand that if i change the function name of the second calculateCircumference
and calculateArea
, the whole code will work but what am i doing wrong that is showing NaN
in the output when both of the function names are the same?
Or is this whole thing just plain wrong and not possible?
Any help is much appreciated, thank you