Using Math.min() and Math.max() to create an input range on a method

I am on the end of the Build a Library challenge in the Learn Intermediate JavaScript course.

I have passed all of the required items, and am working at the end of the challenge (task 25) where it suggests several new things to practice. On the third bullet point, it says “In .addRating(), make sure input is between 1 and 5.” I have watched the tutorial video and in it he suggests using Math.min() and Math.max() to limit the input range on addRating(), and after doing some research I am still not sure how to actually limit the input range from 1 to 5.

So far I have the method as:

addRating(value) {
this.ratings.push(value);

}

but I am not sure what to put below to limit the range. Is anyone able to help me with this? Thank you in advance.

I’m scratching my head as to how or why we would use array methods to test if a value is in range. The methods find the smallest and largest values in an array, which has nothing to do with range except to describe the numerical distance between smallest and largest.

A simple AND statement will do the job nicely:

if (1 <= value && value <= 5) {

}

Thank you for your help! Your answer does make a lot of sense, but I am still struggling to get it correct. Here’s how I have it now:

addRating(value) {
if (1 <= value && value <= 5) {
this.ratings.push(value);
}
else {
console.log(‘rating needs to be between 1 and 5’);
}

1 Like

Are you getting an error message? Please describe.

Also note that your method has no closing brace.

Yes, although there is a lot more code in here so there could have been something else wrong that I’m not noticing. Here’s the error:

Output:
/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:43
class Book extends Media {
^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

also, I did have the closing brace but forgot to paste it there

The error is likely just above line 43 since the class keyword is unexpected. Something is wrong with an earlier line.

1 Like

It was the extra } I was missing! I was accounting for the closing } for the method, but not the Media class. Thank you for your help, it worked this time!

1 Like