1

I have a problem or I didn't get it. I need to write class Circle and implement following functionality.

1.Properties:center coordinates and radius.

2.Define constructor with parameters for initialize object.

3.Define method, which returns length f circumference(L= 2 * π * R).

4.Define method ,which return copy of current object.

5.Define method which converts current state of object to string and return result.

6.Define static method of circumference for given radius.

Here's my decision and I'm stuck.

function circle(radius)
{
    this.radius = radius;
    this.area = function () 
    {
        return Math.PI * this.radius * this.radius;
    };
    this.perimeter = function ()
    {
        return 2*Math.PI*this.radius;
    };
}
var c = new circle(3);
console.log('Area =', c.area().toFixed(2));
console.log('perimeter =', c.perimeter().toFixed(2));
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
Andrey Od
  • 11
  • 1
  • 1
    What part are you stuck on? –  Oct 02 '19 at 16:18
  • I don't know how to define method which converts current state of object to string and return result – Andrey Od Oct 02 '19 at 16:20
  • Refer to this link's answer for what "object state" is. https://stackoverflow.com/questions/18219339/trouble-understanding-object-state-behavior-and-identity Every JavaScript object has a toString() method that you can override. So if for example you wrote this: this.toString = function() { return this.perimeter(); } console.log(c.toString()); console.log(c); They would both return the same result. – Gordon Kushner Oct 02 '19 at 16:34

1 Answers1

0

If you're looking to implement the same functionality with modern javascript (ECMAScript). Here is the below approach

class circle{
    constructor( radius ){
        this.radius = radius;
    }
    
    get area(){
        return Math.PI * this.radius * this.radius;
    }
    
    get perimeter(){
        return 2*Math.PI*this.radius;
    }
}

var c = new circle(3);
console.log('Area =', c.area.toFixed(2));
console.log('perimeter =', c.perimeter.toFixed(2));

If you want to make area and perimeter as method not just getter methods. Then you should drop the get prefix before the method name. As that means these methods are getters

Haseeb Afsar
  • 619
  • 5
  • 7