Adam
Adam

Reputation: 33

JS | Methods | Add the result of two functions in a third function | solution?

Does someone know how to combine two functions in a method? I want to add the result of two different functions in the third function and log it to the console. Thanks for help.

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;
    }
    this.logg = function() {
        return this.perimeter + this.area;
    }
};

var perimeter = new Circle(12);
perimeter.perimeter();
//doesn't work
console.log(perimeter.logg());

Upvotes: 0

Views: 213

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68665

You get the concatenation of the toString result of the functions. You forgot to call the functions - return this.perimeter() + this.area()

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;
    };
    
    this.logg = function() {
        return this.perimeter() + this.area();
    };
};

var perimeter = new Circle(12);
perimeter.perimeter();
//doesn't work
console.log(perimeter.logg());

Upvotes: 2

Related Questions