33/33 Methods


#1


I can't seem to figure out how to make this work. It wants me to create a new method for perimeter, and I've done that. However, when I define radius, it says it is undefined so I remove the radius. Then it says, Oops, try again. Your perimeter method fails on Circle with radius 1 where it returns NaN when it should return 6.283 etc.


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


#2

Follow the example given for this.area, as you have, but include it in the original function, where indicated by the comment.

function ...
    this.radius = ...
    this.area = ...
    this.perimeter = ...

#3

so what I am guessing is that it wants me to add to the original method putting in a way to calculate the perimeter too? because when I do this.radius = ... it comes up with an error message saying that radius is not defined.


#4

this.radius = ...

The horizontal ellipsis indicates the code you already have. I just wanted to illustrate that they are all part of the same function.


#6

I know that, but I did that so I wouldn't have to come up with one. In the example it says this.radius = radius, however when I do that it says radius is not defined. My conundrum is when I add that part of the code, it says radius is not defined, when I remove it I get NaN.


#7

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

Does your code look anything like above? Notice that we have only one class, with three attributes, two of which are methods.


#8

ok nevermind... turns out that I can't have the method set to perimeter. I have to have it set to radius. Heres the code:

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

function Circle (radius) {
    this.radius = radius;
    this.perimeter = function () {
        return 2 *Math.PI * this.radius
    };
};

#9

The second class is overwriting the first so you are losing the area method. Combine them in one class as shown above.


#10

ok, I was a bit confused as I thought it wanted me to create a whole new function.


#11

I trust you get the picture, now? As you progress you will learn how to extend the classes prototype to write methods outside of the class definition (the constructor). It is a more efficient way, but I won't go into details just now. Keep up your studies and happy coding!


#12

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