Recreating Lodash Library - has method - code is throwing error - help pls

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

I’m curious why my code for the Lodash Recreating Library: .has method exercise is throwing a syntax error.

// .has() takes object & key as params, checks if object contains value at specified key, returns true or false, 
  has (object, key) { 
    return object[key] === undefined ?;

Error:

/home/ccuser/workspace/underscore-javascript-capstone/_.js:35
return object[key] === undefined ?;
^
SyntaxError: Unexpected token ;
at createScript (vm.js:53:10)

It’s hard to tell here in my copied-and-pasted what the error’s pointing to, so here’s a screenshot too.

Can someone pls explain what’s wrong with the syntax in my code? And why it’s not a valid solution?

I’d hazard a guess that it’s possibly because you have an incomplete ternary expression?

If a ternary expression is of the form:

(condition) ? (execute if true) : (execute if false);

you’re missing half of the code.

The reason I didn’t include the true : false is because 1) I had a discussion with another discussion forum moderator, and was informed this was redundant, and 2) I thought it was redundant and not necessary to add this since it was understood. I guess I misunderstood (not surprising…I misunderstand most of this stuff it seems).

I’ll go back and revamp this code to include the true : false expression after the ? and, fingers crossed, it’ll work.

Appreciate your help!

If all you want (or need) to return from that function is a boolean value, then yes you can achieve that with just the condition. In which case, your error is the inclusion of the ? - this sets JavaScript up to expect a ternary expression that you’re not completing, and so you rightly get an error from the interpreter.

If your return is:

return object[key] === undefined

then your function will return whatever the condition evaluates to. No ? required. :slight_smile:

2 Likes

Ah, thanks! Appreciate the tip.

1 Like