-2

New learner here, I'm stuck on a practice question for a course and I'm not sure what it's asking me to do here. The problem is as follows:

Write a function that takes a user object, which represents a user of the Northcoders website, and returns the value of the password key from that object.

If the user object does not contain a password key, the function should instead return undefined.

A typical user object might look like this:

{ name: 'Lucy', password: 'n0rthc0derzzz' }

Requirements are as follows:

  • Returns undefined when given a user object where no password key is present - check
  • Returns a password from a user object when a password key is present - unchecked

Starting code is:

function retrievePassword (user) {
// Your code goes here...

So judging by //Your code goes here... I'm not supposed to change the arguments for the function since I'm supposed to write my code below.

Any help much appreciated! Thanks in advance!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Charl
  • 1
  • Check out for bracket notation to access key in an object. – Hassan Imam Aug 26 '22 at 18:46
  • @HassanImam Bracket notation isn't needed, since it's not a dynamic key. – Barmar Aug 26 '22 at 18:48
  • check `hasOwnProperty` documentation. or this link hat is similar https://stackoverflow.com/questions/455338/how-do-i-check-if-an-object-has-a-key-in-javascript – Me Bottle O Scrumpy Aug 26 '22 at 18:49
  • 3
    This is basic property access of an object, it should be one of the first things you learned about objects. Just use `variable.propertyname` to get it. Replace `variable` and `propertyname` with the appropriate names for your function. I can't say much more without doing your homework for you. – Barmar Aug 26 '22 at 18:50
  • 1
    @MeBottleOScrumpy That's not needed. If the property doesn't exist, you'll get `undefined` when you try to read it, and that's what they're supposed to return. – Barmar Aug 26 '22 at 18:50
  • See this tutorial – Barmar Aug 26 '22 at 18:52
  • 0 efforts were taken before asking the question – Konrad Aug 26 '22 at 18:52

2 Answers2

0

I was stuck on this too for a while.

function retrievePassword (user) {
if (user.hasOwnProperty("password")){
  console.log(user.password);
  return user.password;
} else {
  return undefined;
}
}
Jcsg99
  • 1
  • 1
-1
function retrievePassword (user) {
  return user.password
}

or

function retrievePassword ({ password }) {
  return password
}

or

const retrievePassword = ({ password }) => password

or

const retrievePassword = (user) => user.password
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • Please don't do their homework for them. – Barmar Aug 26 '22 at 18:52
  • On top of not doing homework, it doesn't follow the requirements for _presence_ of a key vs what the value is. – chazsolo Aug 26 '22 at 18:59
  • @chazsolo I don't understand what requirement. *If the user object does not contain a password key, the function should instead return undefined.* this is fulfilled – Konrad Aug 26 '22 at 19:04