Clamp challenge

Hi all.

I’ve been working on the lodash challenge project : https://www.codecademy.com/content-items/fb6220182ee4317cc3eaf380b71175de?preview=true

My first implementation passed the test, but differs from the provided solution. My question is, is my solution valid, and if so would it be better/worse than the provided solution, or make no practical difference? My solution is below:

const _ = {

clamp(x,lower,upper) {
    x = Math.max(x, lower);
    x = Math.min(x, upper);
    return x;
  }

}





// Do not write or modify code below this line.
module.exports = _;

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Your solution is fine. Really no practical difference. The provided solution preserves the original value of the number to be clamped, but there’s no requirement or benefit to doing so.

2 Likes

Many thanks for taking a look.

1 Like