Lodash, implementing _.clamp()

Hi, I’m working through the lodash project. On the “implementing _.clamp()” section, when I enter my code and test it with:

node test/lodash.js

It passes. However, when I actually run the function i created:

const _ = {
  clamp (number, lower, upper) {
    const lowerClampedValue = (number, lower) => {
      return Math.max(number, lower);
    }
    const clampedValue = (lowerClampedValue, upper) => {
      return Math.min(lowerClampedValue, upper);
    }
    return clampedValue
  }
}

console.log(_.clamp(15, 0, 10));

it returns:

[Function: clampedValue]

not the value im expecting: 10.

what am I doing wrong?

exercise link is:
https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash

Both your lowerClampedValue and your clampedValue are functions. When you return clampedValue you are returning the function. These should both be variables assigned the values returned by Math.max() and Math.min().
Should look more like this:

clamp (/*parameters*/) {
    const lower = Math.max(/*parameters*/);
    const clamped = Math.min(/*parameters*/);
    return clamped;
}
4 Likes

A clamp only has one sense of direction, derived from the number line. Lower on the left, higher on the right.

Something our lodash method will have to be able to do is assess the inputs and be sure they are placed in the correct order.

Given parameters a and b,

if ( a > b) {
  c = b;
  b = a;
  a = c
}
2 Likes

aah, thank you. I get so confused on how or what form of function to use. It seems like there’s a lot of different ways to write functions depending on where they’re placed and such. I thought the form I was using saved the return value to the variable but I guess the whole function is being saved to it. It did seem odd using two sets of the same parameters. thanks for clearing that up for me.

1 Like