Lodash object project


in this project of lodash project that hire to create an object called (_) and in this project we add a clamp method that had three argument like this :

module.exports = _;
const _ = {
clamp(number, lower, upper) {
let lowerClampedValue = Math.max(number, lower)
let clampedValue = Math.min(lowerClampedValue, upper)
return clampedValue
}
.clamp(4, 5, 6)
then i use to run this code in the tereminal as that “node test/clamp.js” but it return me:
$ node test/
.clamp.js
module.js:472
throw err;
^

Error: Cannot find module ‘/home/ccuser/workspace/underscore-javascript-capstone/test/_.clamp.js’
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module.load (module.js:418:25)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
$ node test/
.clamp.js
module.js:472
throw err;
^

Error: Cannot find module ‘/home/ccuser/workspace/underscore-javascript-capstone/test/_.clamp.js’
at Function.Module.resolveFilename (module.js:470:15)
at Function.Module.load (module.js:418:25)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
4,5,6 bash: 4,5,6: command not found node test/clamp.js
/home/ccuser/workspace/underscore-javascript-capstone/
.js:15
console.log(
.clamp(5, 4 ,6))
^^^^^^^
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.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/test/clamp.js:1:73)

Sometimes you’re trying to run the test with the wrong filename. When you’re running it correctly, you’re getting a different error because of your code.

Incorrect name

Correct name


If you want to test your clamp() method, then remember you need to use the object name too. I know it’s a little strange because its’s called _, but that’s still the object name.

Example of a valid call

console.log(_.clamp(4, 5, 6));

i do the correct name node test/clamp.js but it return the same error :

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.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/test/clamp.js:1:73)

Post your complete code again, but remember to format it so we can see it properly in the forums

image







module.exports = _;
const _ = {
  clamp(number, lower, upper) {
let lowerClampedValue = Math.max(number, lower)
let clampedValue = Math.min(lowerClampedValue, upper)
return clampedValue
}
console.log(_.clamp(5, 4 ,6))

You’re missing a curly brace to let JavaScript know where to end your object. }

excuse me friend but my () object is defined but it return me this Error:
ReferenceError: _ is not defined
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/
.js:7:18)
at Module._compile (module.js:571:32)
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.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/test/clamp.js:1:73)
at Module._compile (module.js:571:32)

Your module.exports line is supposed to be at the bottom. That’s why it originally had this comment when you started the project:

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

Your code goes above that. Otherwise, _ doesn’t exist yet

i had follow your advise to rewrite the code like this :


// Do not write or modify code below this line.

const _ = {
  clamp(number, lower, upper) {
let lowerClampedValue = Math.max(number, lower)
let clampedValue = Math.min(lowerClampedValue, upper)
return clampedValue
}
}
console.log(_.clamp(4, 5, 6))
module.exports = _;


```but the self error return always 
ReferenceError: _ is not defined
    at Object.<anonymous> (/home/ccuser/workspace/underscore-javascript-capstone/_.js:7:18)
    at Module._compile (module.js:571:32)
    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.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/ccuser/workspace/underscore-javascript-capstone/test/clamp.js:1:73)
    at Module._compile (module.js:571:32)

Did you remember to press Save? I just copy/pasted it into the project to test it:

Thanks for your help and your openness my friend