Accessing a property using argument returns undefined

For this challenge I need to access whatever property is specified as the second argument (pre) of the function, I couldn’t figure out how to do it so I used a map method as a workaround, returning a specific value of an object if the type of its name is a string. It works when I type a.isBot, but not when I type a.pre, even though pre equals the string of "isBot". Why is this happening? Also, if someone could share how to access specific values I would really appreciate it.

function truthCheck(collection, pre) {
  return collection.map(a => typeof a.name === 'string' ? a.pre : null)
}

console.log(truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "isBot"));```

What do you mean? Why is it not returning a.pre?

Please link to the challenge when you post on the forum so we can help out.

The pre argument to the function is an arbitrary string value. Remember how to access object properties at arbitrary string values?

a.pre ← this tries to access the property literally called “pre” on the object “a”, which is not what you want (I think).

2 Likes

I think you need bracket notation for that.

2 Likes

Yes, that solved the issue and now the function satisfies 9 out of 10 conditions. Could you tell me what the problem is now?

function truthCheck(collection, pre) {
  let x = collection.map(a => typeof a.name === 'string' ? a[pre] : null)
  return x.every(e => Boolean(e))
}

Do you need to use both map() and every()? I bet you can solve this with only every().

Well it’s not even passing as it is lol, that’s where I’m stuck right now.

Do the instructions say anything about checking the name property on each object?

No but that’s the only way I know of to access specific values. Can you tell me why the function is not passing?

Edit: oops, accidentaly overwrote this comment. Sorry! Check the original comment by clicking the pencil icon.

All the values at name are strings so I’m getting their values at pre and checking them for truthiness/falsiness. That’s my logic. Again, I’m using this method only because I don’t know of any other way to access values at pre. So why is the function passing for 9 conditions out of 10?

All the values at name are strings so I’m getting their values at pre

This doesn’t make sense. In your function, a is an object. You’re checking two properties on that object when the requirements only ask you to check one.

With the above transformation function, and a pre value of “age”

// this transforms to 39
{
  name: 'Bob',
  age: 39
}

// this transforms to null (because `a.name` is not a string)
{
  age: 39
}

// this transforms to null (because `a.name` is not a string)
{
  name: 1234
  age: 39
}

Does that match your expectations?

Looking at this I don’t know what I’m supposed to expect. All I know is I need to access values at pre and pass them through Boolean(), and fetching them by mapping is the only way I know of.

Exactly! You should do that, and only that. Get rid of the fluff (checking a.name) and you’re good!

PS I’d expect them all to transform to 39, the value of a[pre].

I don’t know how to access them :face_exhaling:

Huh? You’re already doing it. Right here:

a[pre]

I don’t know where to go from there

Have you updated your code yet? Please post it again if you’ve made any changes.

As a first step, just try to log out the pre value of every object in the array.

I took a look at the solutions and rewrote my code. I was pretty close to solving this though. Thanks for your help.

function truthCheck(collection, pre) {
  return collection.every(element => Boolean(element[pre]));
}