Array of Object having issue with getting true property

Hi,

I need help with this. I am getting an error and don’t understand why.

Here is the question
"This is an extension of question #2. Given an array of objects, write a function admin that returns the name and birthdate of all users that are marked as “admin”. "

below is the error
-Failed tests

handle_given_input
ReferenceError: admin is not defined

handle_additional_input
ReferenceError: admin is not defined

Below is my code

// Write your admin function here

const users = [
  {
    name: 'Homer', 
    role: 'clerk', 
    dob: '12/02/1988',
    admin: false 
  }, 
  {
    name: 'Lisa', 
    role: 'staff', 
    dob: '01/30/1965',
    admin: false 
  }, 
  {
    name: 'Marge', 
    role: 'associate', 
    dob: '09/10/1980',
    admin: true 
  }
]
function namesAndRoles(users) {
   input = '';
  
 for(let key of users) {
   if ( key['admin'] === true){
   Name = "Name: " + key.name
   Dob = "\nDob: " + key.dob
   input += Name + Dob + "\n\n"
   }
  }
 
 return input;

}

output = namesAndRoles(users);
console.log(output);

// Name: Marge 
// Dob: 09/10/1980

you missing something in declare variable. let input, var Name, var Dov , var output. declare those variable in correct way. t

Hey @Greg300,

Have you tried using the dot-notation instead of the bracket notation. so instead of key['admin'], just use key.admin

I tried both from and from the documentation I have read, the dot or bracket notation would not be problematic.

I am not sure why that matters here. The only variable here is the input for setting up for the string to be returned.

All the object, keys and values are all in the array.

Is this a challenge? if so can you link it here.

Is this the full code, because when I tried it myself in JSFiddle , it worked perfectly fine.

Yes, it’s a challenge.

Here is question #2
Given an array of users, write a function, namesAndRoles that returns all of user’s names and roles in a string with each value labeled.

my answer to #2



 const users = [
  {
    name: 'Homer', 
    role: 'Clerk', 
    dob: '12/02/1988',
    admin: false 
  }, 
  {
    name: 'Lisa', 
    role: 'Staff', 
    dob: '01/30/1965',
    admin: false 
  }, 
  {
    name: 'Marge', 
    role: 'Associate', 
    dob: '09/10/1980',
    admin: true 
  }
]

function namesAndRoles(users) {
   input = '';
  
 for(let key of users) {
   Name = "Name: " + key.name
   Role = "\nRole: " + key.role
   input += Name + Role + "\n\n"
  }
 return input;

}

output = namesAndRoles(users);
console.log(output);

Question #3 is the original post
This is an extension of question #2. Given an array of objects, write a function admin that returns the name and birthdate of all users that are marked as “admin”.

Also, I can run it just like you can (I use repl.it) but when I go to submit it it throws these errors.

-Failed tests

handle_given_input
ReferenceError: admin is not defined

handle_additional_input
ReferenceError: admin is not defined

Where do you submit this? Which challenge is this? Can you link the challenge here?

Here on your new code, you forgot to check if the user have key['admin '] == true is this the code that you submitted?

No, question #2 just wants the names and roles from the array-object which my code passed fine.

In question number #3 is where I took the code from my answer in #2 an added an if statement so it would only return the admin that were true.
Of course change Role to Dob as well

Try clearing your browser cache and if it still doesn’t work, try running it on an incognito mode. Can you maybe give us a screenshot of the error occuring

could someone help me my code isnt working thx

simplified version:

function admin(users){
   return users.filter((user) => (user.admin === true)).map(x => ("Name: " +x.name+" DOB: " +x.dob))
}

you have not created such function

Hello there, @kojicephas
If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Occams Razor- The simple answer is most likely the correct one.

After sleeping on this I realized
I forgot to change the function namesAndRoles (users) in my 2nd challenge question to function admin(users) for my 3rd challenge question.

Thank you for helping me realize the answer remains in front of us usually.

const users = [
  {
    name: 'Homer', 
    role: 'clerk', 
    dob: '12/02/1988',
    admin: false 
  }, 
  {
    name: 'Lisa', 
    role: 'staff', 
    dob: '01/30/1965',
    admin: false 
  }, 
  {
    name: 'Marge', 
    role: 'associate', 
    dob: '09/10/1980',
    admin: true 
  }
]

function admin(users) {
  input = '';
  
for(let key of users) {
  if ( key['admin'] === true){
  Name = "Name: " + key.name
  Dob = "\nDob: " + key.dob
  input += Name + Dob + "\n\n"
  }
  }
 
return input;

}


output = admin(users);
console.log(output);

// Name: Marge 
// Dob: 09/10/1980

I tried your code and it works like a charm. No errors came up

1 Like

This might work nicely:

function admin(usrs) {
    return usrs
        .filter(u => u.admin)
        .map(u => u.name + " " + u.dob);
}

Good luck
-Rob