Intermediate JavaScript building a library - Function returning Function name and not the output?

When I compile this code I would expect line 70 to log to the console this average of the array im passing. In stead I simply get the name of the function “[Function: getAverageRaiting]”. What am I missing here?

Learn Intermediate JavaScript – Build a Library

class Media { constructor(title) { this._isCheckedOut = false; this._raitings = []; this._title = title; } get isCheckedOut() { return this._isCheckedOut; } get raitings() { return this._raitings; } get title() { return this._title; } toggleCheckOutStatus() { this._isCheckedOut = !this._isCheckedOut; } getAverageRaiting() { let raitingsSum = this.raitings.reduce( (accumulator, raiting) => accumulator + raiting ); return raitingSum / this.raitings.length; } addRaiting(newRaiting) { this._raitings.push(newRaiting); } } class Book extends Media { constructor(author, title, pages) { super(title); this._author = author; this._pages = pages; } get author() { return this._author; } get pages() { return this._pages; } } class Movie extends Media { constructor(director, title, runTime) { super(title); this._director = director; this._title = title; this._runTime = runTime; } get director() { return this._director; } get runTime() { return this._runTime; } } const historyOfEverything = new Book( "Bill Bryson", "A Short History of Nearly Everything", 544 ); historyOfEverything.toggleCheckOutStatus(); historyOfEverything.addRaiting(4); historyOfEverything.addRaiting(5); historyOfEverything.addRaiting(5); console.log(historyOfEverything.getAverageRaiting); console.log(historyOfEverything.isCheckedOut); console.log(historyOfEverything);

corrected a small type in the function in the parent class. but same results.

getAverageRaiting() { let raitingsSum = this.raitings.reduce( (accumulator, raiting) => accumulator + raiting ); return raitingsSum / this.raitings.length;

ha, further typos and some playing around and I figured it out. (i’m dyslexic)

I was omitting the () after the function name. I dont fully understand why this is needed since I didnt need it on the other functions like isCheckedOut?

class Media { constructor(title) { this._isCheckedOut = false; this._ratings = []; this._title = title; } get isCheckedOut() { return this._isCheckedOut; } get ratings() { return this._ratings; } get title() { return this._title; } toggleCheckOutStatus() { this._isCheckedOut = !this._isCheckedOut; } getAverageRating() { let ratingsSum = this.ratings.reduce((accumulator, rating) => accumulator + rating); return ratingsSum / this.ratings.length; } addRating(newRating) { this._ratings.push(newRating); } } class Book extends Media { constructor(author, title, pages) { super(title); this._author = author; this._pages = pages; } get author() { return this._author; } get pages() { return this._pages; } } class Movie extends Media { constructor(director, title, runTime) { super(title); this._director = director; this._title = title; this._runTime = runTime; } get director() { return this._director; } get runTime() { return this._runTime; } } const historyOfEverything = new Book( "Bill Bryson", "A Short History of Nearly Everything", 544 ); historyOfEverything.toggleCheckOutStatus(); historyOfEverything.addRating(4); historyOfEverything.addRating(5); historyOfEverything.addRating(5); console.log(historyOfEverything.getAverageRating()); console.log(historyOfEverything.isCheckedOut); console.log(historyOfEverything);

When you create a method using the get accessor, accessing those properties is treated like a property of the object rather than a method, syntax-wise at least. In other words, if I create a method like get property()..., I access the property with object.property. But if I create a method without get, I access the method and call it i.e. object.getAverageProperty()

2 Likes