Build a Library Help


#1

Hi there!

I am having a little bit of trouble with build a library exercise in Classes lesson.
Link:
https://www.codecademy.com/courses/learn-javascript-classes/projects/build-a-library?course_redirect=introduction-to-javascript

I am trying to get step #6 done by “using the reduce method to find the sum of the ratings array & dividing this sum by the length of the ratings array, and return the result”

getAverageRating() {
let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
const lengthOfArray = _ratings.length;
return this.ratingsSum / this.lengthOfArray;
}

anybody can tell how to fix it?

Many thanks in advance!


#2

EDITED December 1, 2017 to recommend using this with _ratings

Hi @betarunner60927,

You have this line of code …

const lengthOfArray = _ratings.length;

In the above, use this with dot notation to access the _ratings variable.

My version of the getAverageRating method also departs somewhat from the advice given in the hint …

getAverageRating() {
    let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
    return ratingsSum / this._ratings.length;
  }

#3