Library

Hello All,

I am doing ok with this project but i have some small questions:

line 29-30

let sum = this.rating.reduce((currentSum, rating) => currentSum + rating, 0);
return sum/this.rating.length;

why can’t I say rating.reduce() or rating.length?

line 34

return this.rating.push(newRating);

the hints and tutorial didn’t use return, i used it and it works. may i know when to use and when not to, and what’s the difference?

ThankS!

wrt 29-30: This is because we’re defining a class. And a getter function is accessed as a property (i.e. we don’t access it using rating() ). And inside a class, we must refer to properties using ‘this’.

wrt 34: with this method the only requirement is to update the rating array. Unless you have a special reason to be returning a value (in this case the new length of the array returned by array.push), then you don’t need to specify ‘return’. It will work, but it’s redundant here.

It would be like using ‘return’ in a setter function; the purpose of the function is to update the property, not to fetch and return anything to the calling program logic.

Be careful how one draws conclusions. The statement works and no error is raised because nothing is expected after updating the rating. As mentioned above by @antonjw, there is a return value, the new length of the array whick slips by quietly in the background.

On the assumption that everything works we could be introducing bugs into the program that could be very hard to track down.

ok, So we don’t return an update?

yes i can see that, i guess my problem now is usually that i can’t tell whether it is sufficient to just update and when need to be return. I guess i am having some problem on the definition of all the terms. i will look into it. thanks!!

yeah exactly. Not from this function.

In summary: ‘return’ passes a value from your function to the calling code, which you can use.
It is:

required in Getters
not used in Setters
your call in any other function (in accordance to your program’s needs)

In this case, if you had wanted to know the new length of the rating array, and you needed to store that in a variable, like:

let newSize = speed.addRating(3);

then

console.log(newSize);

would print the increased size of the array to the console; it would do that because you had done:

return this.rating.push(newRating);

in your addRating() function.

It didn’t break anything that you didn’t use the returned value. It just wasn’t needed.

My advice: If you don’t need to return a value, then don’t write ‘return’ - and for your own sanity it might be best not to use it! (like when you come back to your code and wonder to yourself why is it returning a value, and where are you using that in your code? is it needed? will i break something if i remove it?). Use as needed.

Hope it helps.

I am watching the tutorial, for the part of adding rating,

I think the tutor said something opposite?

For the time 7:00, he said ‘‘we are using a method instead of a setter cos we are assigning a value to the ratings property, changing the value already stored in the property, if we are assigning a new value we will be overwriting the whole array’’

but at the time 7:30, he said ‘‘that’s why we are not using setter for the array, we don’t want to overwrite the whole array, we are just adding one new value to the end of it’’

aren’t these two sentences contradictory?

Thanks!!!