Methods: How do i find the perimeter of a circle with a specific radius


#1

In the below, how do i find the perimeter of a circle with radius 5

function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
        
    };
    // define a perimeter method here
    this.perimeter = function(){
        return 2* Math.PI * this.radius
        }
      console.log(Circle.perimeter(5));<--- <<<is anything wrong with this line?>>>
      
    };

#2

Your perimeter method takes no parameters meaning you need to call it outside of any brackets but without any perimeters,

console.log(Circle.perimeter)


#3

I tried it but the console showed 'undefined '


#4

Ohh you mean you want it to pass arguments and make it return values?


#5

In order to do that you have to create an instance of your Circle object and call the radius or perimeter function with it for example,

circle = new Circle(360)
circle.perimeter()

#6

this line:

console.log(Circle.perimeter(5));

is faulty, there are two problems, it is at the wrong place and it is not correct. First we create our object:

function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
        
    };
    // define a perimeter method here
    this.perimeter = function(){
        return 2* Math.PI * this.radius
    }
};

now we created our object we need to create an instance of the object. in short:
an object describes an item
a instance is the actual item

so now we need to create an instance, you learned this:

circle = new Circle(5)

so now we have a circle with a radius of 5, now we can get the perimeter:

console.log(circle.perimeter())

#7

Thanks... it worked!!!....really helpful.


#8

I get it now... I'm to "redefine" the function before I call it. ...My mistake...Thanks. i still have work to do on functionI


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.