Re-creating the Lodash Library - Implement _.has() - Need help


#1

Link to project: https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash

I’m currently stuck at tasks 19 to 22 _(Implement .has())

Below is my current working code for this particular step (I cut out previous code for better visibility)

const _ = {

has (object, key) {
const hasValue = Object.keys(object) !== undefined ? true : false;
return hasValue;
}

};

This code isn’t correct, but it’s where I’m currently at.

I get the feeling that the solution to this step is simple, but I’m just not getting it right. There are no hints in this project and there is no way to check the final code, so I feel very stuck & unproductive, hence why I’ve decided to ask for help here.

Basically I’m having trouble with Step 3 of Task 21. which says the following:

Within the method, create a variable called hasValue and set its value equal to the result of checking to see if the current value of object at key does not equal undefined .

The two parameters have thrown me off and I’m not sure how to properly check the value of ‘Object’ at ‘key’ as the instructions say. The above code is my best attempt, but it doesn’t fully pass the test run on task 22.

I will add a screenshot here so you can see what the error is:

https://drive.google.com/file/d/1x65P6j6E0Edh-Xzz8R0TKaJsm0Giia83/view?usp=sharing

Any help?


#2

Object.keys() is not a value, but an array of keys.

This should work since it returns a boolean…

var hasValue = object.hasOwnProperty(key);
return hasValue;

Mind, it doesn’t test the value associated with the key. Is there any chance that the value will be one of the following?

zero (0)
'' (empty string)
""
null
false

Assuming none are present, we can narrow down to undefined being the only falsy left in the list.

var hasValue = object.hasOwnProperty(key) ? (object[key] ? true:false) : false;
return hasValue;

return object.hasOwnProperty(key) ? !! object[key] : false;

This was my test object…

object = {one: 1, two: 2, three: undefined}
 > ['one', 'two', 'three', 'four'].forEach(key => console.log(object.hasOwnProperty(key) ? !! object[key] : false))
 2 true
 2 false
 > q = []
<- []
 > ['one', 'two', 'three', 'four'].forEach(key => q.push(object.hasOwnProperty(key) ? !! object[key] : false))
<- undefined
 > q
 > (4) [true, true, false, false]

#3

Thank you for the help mtf.

Unfortunately I will have to come back to this project tomorrow, but I definitely would never have thought to use the object.hasOwnProperty. I don’t recall ever having used this method in any of the codecademy lessons.

Is there perhaps another way to solve this without having to memorize particular methods?


#4

I could be way out in left field, but still looking at this particular problem. I don’t have the lesson, just your code and scenario as described. Looking at a way to utilize the keys array.


Testing on the Object.keys() array means we don’t have to check for own property.

 > r = {}
<- {}
 > (Object.keys(object)).forEach(key => r[key] = !! object[key])
<- undefined
 > r
<- > {one: true, two: true, three: false}

The question always has been, does the key exist, or is that the objective of the function. The name would suggest the key does exist, but may be undefined. If the key may have any of the falsy values discussed earlier then a specific comparison will be needed else there could be some errant outcomes.

I still don’t know the context of this problem but that is what has made it fun to explore.

If we know that the key exists (it has been captured in a for key in object loop) and wish to see if it has a value that is not falsy,

has (object, key) {  // truthy value
  return !! object[key];
}

If we wish to confirm the presence of a key,

has (object, key) {  // key
  return object.hasOwnProperty(key);
}

Now, if we need to narrow the selection down to just undefined, then,

has (object, key) {  // value
  return object[key] !== undefined;
}

 > const _ = {
     has (object, key) {
       return object[key] !== undefined;
     }
   }
<- undefined
 > _.has({one: true, two: true, three: false}, 'three')
<- true
 > _.has({one: 1, two: 2, three: undefined}, 'three')
<- false
 > const _ = {
     has (key) {
       return this[key] !== undefined;
     },
     one: 1,
     two: 2,
     three: undefined 
   }
<- undefined
 > _.has('three')
<- false

Aside

Earlier I touched on undefined but felt it went over the line and was incorrect, so I edited it out. undefined bears examining a little further because of its unique nature.

undefined

Note that in an object, we cannot leave a key value pair hanging like we can with var.

var a;
console.log(a);    //  undefined

If we write a key that has no corresponding value, then we get this,

Uncaught SyntaxError: Unexpected token }

if it’s the last key in the object, or an unexpected identifer if somewhere else in the object. Values must be defined, else the variable undefined must be used.

Again it goes back to context, which I have not as yet sought out. Still fudging with the concepts and getting the kinks out of my thinking.

Which value is better to assign? undefined or null?

null is the top of the prototype chain. Like undefined it is just a primitive glued to the global object. But it turns up in a lot of places where undefined would be a poor fit.

Bottom line, JS has no problem binding something (undefined) to a declaration, but it is powerless in binding to key. We have to have something there. Let logic dictate whether we use null or undefined. null, by the way, bears some investigation, as well.


For my own satisfaction, I see undefined as generated by the interpreter in a variety of cases. null, on the other hand is usually an assignment, which can be interpreted as something arbitrarily set on a return value of a method or function. undefined cannot be interpreted thus. Therein lies the distinction.


var user = prompt("Click `Cancel` or press `Esc`")

console.log(user)    //  null

Note that this is a value that can be tracked. We can use this as a control flow reference that has a distrinct meaning. The user cancelled the prompt and we should therefore handle that response rather than let the program continue along its normal path.

Yes, there are cases where undefined would offer this logical leverage, but in this case, I agree with the JS developers who opted to use null in this circumstance. These questions do come up in a complex problem. The right choice is often very gray.


#5

No mtf. No, it hasn’t.


#6

Once we start to think for ourselves, meaning we can start a project and visualize a plan, follow it and reach some conclusion, then the fun begins. We may fumble, we may stumble, but one thing is for sure, we learn and discover new things to expand our knowledge.


#7

Very inspiring mtf.

My experience is that if you’re really passionate about something, you’ll stick with it and eventually pull through, no matter what it is.

If you wanted to know what the context of this project was, all we’re basically doing is creating methods from scratch (in this particular case, we are recreating certain methods from the Lodash library which can be found here)

Thanks again for the help mtf. I especially appreciate the time you take to explain your process.


#8

Was that link always at the top of the topic? If so, my bad for being so blind to it, else I mightn’t have strayed so much afield. I did find and complete the project a couple days later. Interesting, and fun.


#9


im stucked at this point. someone please just give me some help here im completely beginer with html java and css and i managed to get here with lots of strugle .


#10

Did the platform somehow land you on this project or did you select it yourself?


#11

it just land me there sir but i have folowed the html css and the intermediated javascript but didnt had this until now. never saw it before .


#12

In your dashboard open all the units by clicking on their heading. Are there any that are incomplete with others completed after them?


#13


as u see sir it is number 5


#14

Looks pretty natural. Did the lodash project come up at the end of unit 5? (Click on View Detail to see if it is in that list.)


#15


well as u see it is here is the last lesson from number 5 lesson but stil im realy stucked im on it for like 40 min and coundt continue because the instructions made no sense at all and its the first time i see .clamp if that make any diference .


#16

btw thank you for time trying to help me realy apreciate that .


#17

here in brazil is 4 in the morning so i need to sleep i will look later to see if u replyed thanks for now .


#18

Good morning. This project comes up after the Tea Cozy project. By now you will have gotten to know your way around. We’re asked to exploit that knowledge, here. Deduce what you can from the presents given one’s understanding of objects.


#20

Hi All,

I’m a bit confused by this project. Maybe because I’ve never used this terminal-testing yet.

Just to see what this project is about, I followed the step-by-step instructions to implement .clamp(). Then ran ‘node test/lodash.js’ and got this:

Can someone help me what this is supposed to look like in the terminal? For starters, “_” is undefined so I guess whatever I do further on I will get this error message. I guess the code must be right as it is just copied over from the instructions. How does this terminal testing work? What am I supposed to see and how do I know that my code works? So far, running stuff in the console has been very straightforward but this seems to be a different cup of tea.

Many thanks


#21

There is no test function for lodash.js, but there is one for clamp.js

node test/clamp.js