I am coding a JS class which holds a price
number and a ratings
array of numbers. I have already written a getAverageRating
method that returns the average of the numbers in ratings
, and am now trying to code a valueMeter
method that takes the average of every value in ratings
, divides it by the price
, and multiplies it by a hundred (no comment on the relevancy of such a method). Rather than re-writing the code to calculate the average, I am trying to use my getAverageRating
method.
class Media {
constructor(price) {
this.ratings = [3, 4, 5];
this.price = price;
}
getAverageRating() {
let sum = 0;
let amt = 0;
this.ratings.forEach((rating) => {
sum += rating;
amt++
})
return sum / amt;
}
valueMeter() {
let value = (this.getAverageRating()/this.price) * 100;
return value;
}
}
You can see I tried calling this.getAverageRating()
inside of valueMeter
, but calling the valueMeter
method logged ‘NaN’.
const obj = new Media(30);
console.log(obj.valueMeter());
# logs 'NaN'
I expected the issue to come from the division, but even if I try to return only the this.getAverageRating
call, it is still outputting ‘NaN’.
...
valueMeter() {
let value = this.getAverageRating;
return value;
...
console.log(obj.valueMeter());
# still logs 'NaN'
user25173426 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.