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


#22

Many thanks for your answer mtf. It did help :slight_smile:

On .has() I’d have a question too. I have the following code:

has(object, key) {
return object.key != undefined ? true : false
},
}

and it does not pas the test although it’s not dissimilar to your code further above. The terminal says that ‘has was not proper defined’. Is this because I did not include any keys in the object for the method to run on?

Thanks a lot


#23

Don’t think so… It may have to do with the dot notation. Did you try using a subscript?

object[key]

#24

yeah, I did. same result. I don’t think the code is correct though, I’m firing it at a console with a test object to see what it does and keep getting stupid error messages of unexpected tokens and the rest of it. frustrating.


#25

Errors can appear elsewhere that trip things up. Please post your entire _ object so we can inspect it.

Something else to consider.

return object[key] != undefined ? true : false

This is not a case where a ternary is of any importance. The expression is already boolean.

return object[key] !== undefined;

will return true or false. Note the operator is strict. Avoid using loose comparisons, == or != as this can lead to potential problems down the road.


#26

Hi mtf,

follow-up on this. This code worked:

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

but this one didn’t:

has(object, key) {
return object.key !== undefined
},

I will steer clear of dot notation from now on when it comes to accessing keys in an object.

Many thanks…


#27

Hi all,
I have this kind of situation could someone figure out what happens here? And why I have this error?


#28

The last line in the editor window seems awry. Should it be something like this,

module.exports = _;

?


#29

Hi, I was also stuck on question 21 :

My code worked when I used instead of dot notation as you showed. But I was reading through the replies here and I’ve never come across:

What does the - ? and - true : false syntax mean? I can’t find explanations of it on google.
My answer:

has(object,key){
    var hasValue = true;
    if (object[key] !== undefined){
     return hasValue = true;
    } else {
      return hasValue = false;
    };
    return hasValue;
    }
 

Even though its passed on the console, I didn’t feel I’d gained enough understanding on this one. I was also stuck with the interaction between variables and conditional statements/booleans. Can you not directly assign a variable to an if statement? Or in other words what are the rules for assigning variables to conditional statements?
I was getting errors for:

has(object,key){
var hasValue = if (object[key] !== undefined){
return true;
} else {
return false;
};
return hasValue;
}
Any help would be great


#30

Look up ternary expression statement, which is similar to if..else and can even be expanded to emulate, if..else if..else though it can get messy and convoluted.

ternary means three.

condition ? the_if : the_else;

? is referred to as the ternary operator, and the colon is a required separator token between the two case actions.

We know that only a truthy evaluation makes it past if, and defaults to else for all other cases.

No, since if has no return value, only one of a single or more actions that occur within its blocks.


#31

Ah ok the ternary operator makes sense, thanks again.

I sort of understand what you mean by the lack of return value in an if statement. But why does a function allow variables to be assigned to it directly if it also comprises of actions?
They both use the keyword return?


#32

Functions are objects, as in they are values. IF is a control flow construct and not an object as such. We can return a function, but not an if statement.

Regardless what goes on inside a function, the caller sees only the return value.

const ab = (a, b) => a * b;

We see ab(6, 7) but the caller sees 42.

Now ternary expressions on the other hand are values, so we can assign them.

a = 6; b = 7;
console.log(ab(a, b) === 42 ? "One factor is divisible by 3" : "Not sure if any factors are divisible by 3");
// One factor is divisible by 3

That last line is what the console sees, and logs it.

Because a ternary is a value, we can return it, unlike an if statement.


#33

Ok, so I think I understand: an empty function is in a sense still an object - acting as a placeholder for results/values anyway. But an empty if statement on its own is unrecognizable to a variable?


#34

Precisely. A function, being an object, can be referenced. Try this…

const print = console.log

print("Do you think this will log to the console?")

We just assigned a reference to method, and now invoke it using that reference. This is not something we can do with IF (or WHILE, or SWITCH) as they are control flow devices, not objects.

There is no way to write,

a = if(b) {

}

Not just an empty function, any function. They are objects, and as such, values.


#35

line 4 should be
let clampedValue = Math.min(Math.max(number, lower), upper);
return clampedValue;
},


#36

hiya! reading your responses throughout this project has helped me a ton! So, thank you! i seem to be stuck at this point. I’m not sure how to word my code in part 21.3. ( 1. 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 .) I tried const hasValue = (object.valueOf(key) === !undefined);
** return hasValue;** this got me a lot closer but I’m not quite there. I know the answer is in the instruction itself, but I am just not seeing it. any help you can provide would be much appreciated!


#37
   !==

Equal to NOT is the same as not equal to, and likely easier logic to sort through.