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!
CodePudding user response:
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;
}
}
CodePudding user response:
function retrievePassword (user) {
return user.password
}
or
function retrievePassword ({ password }) {
return password
}
or
const retrievePassword = ({ password }) => password
or
const retrievePassword = (user) => user.password
CodePudding user response:
var hh = ({ name : 'Lucy', password: 'n0rthc0derzzz' });
function retrievePassword(user) {
if (hh.hasOwnProperty(user)) { return (hh?.password) }
}
console.log(retrievePassword("name"))
Yeh, I steel stuck here but I can't find the way instead of "name" put "Lucy"