Build a Library

Hi!
I have a question regarding [Build a Library project ]
( https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/learn-javascript-classes/projects/build-a-library).
Task 25, where you are asked to make sure input is between 1 and 5 in .addRating(). We are advise to use the Math.max() and Math.min() functions. I read the documentation but I don’t know how to implement them. I also find this code:

function getMaxOfArray(numArray) {
  return Math.max.apply(null, numArray);
}

but I don’t know how to implement this in my case. Can anyone explain this to me?

Yup! Using Math.max() and Math.min() can be confusing when you have your numbers stored as an array. Long story short, you want to use the spread syntax to be able call max and min without it giving you a NaN (I’m assuming this is where you are getting caught up).

For example:

const myNumbers = [0,4,2,1,3];
let highestNumber = Math.max(...myNumbers); //expected 4

I don’t think you need that second example with the getMaxOfArray(numArray) once you figure out how to call Math.max() and Math.min() :+1:

I don’t understand the logic behind it. Because the Math.max() and Math.min() are functions that
return the greatest and the lowest of the arguments. They are not checking if the input is between two numbers(in this case between 1 and 5) if I understand correctly.

My logic is this: if the rating is >= 1 and <= 5 then the program should run the average rating. If not, maybe show some text that says something like ‘You have to input only numbers from 1 to 5’, or show some error.

But my logic and the Math.max() and Math.min() functions do not meet. :frowning_face:

1 Like