Reputation: 33
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
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