Profile lookup - ternary operator

Tell us what’s happening:
Hey guys! i tried to use the ternary operator for this task and my code is not working(( could you please tell me what is wrong? thank you very mych!

  **Your code so far**

// Setup
const contacts = [
{
  firstName: "Akira",
  lastName: "Laine",
  number: "0543236543",
  likes: ["Pizza", "Coding", "Brownie Points"],
},
{
  firstName: "Harry",
  lastName: "Potter",
  number: "0994372684",
  likes: ["Hogwarts", "Magic", "Hagrid"],
},
{
  firstName: "Sherlock",
  lastName: "Holmes",
  number: "0487345643",
  likes: ["Intriguing Cases", "Violin"],
},
{
  firstName: "Kristian",
  lastName: "Vos",
  number: "unknown",
  likes: ["JavaScript", "Gaming", "Foxes"],
},
];

function lookUpProfile(name, prop) {
// Only change code below this line
return (name === contacts.firstName && contacts.hasOwnProperty(prop)) ? contacts[prop]
: (name !== contacts.firstName) ? 'No such contact'
: 'No such property';
// Only change code above this line
};

console.log(lookUpProfile("Akira", "likes"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

first thing first, what is contacts? what kind of data structure?

1 Like

Hi Ilenia!

in this case it is an array of objects. so, i should use square brackets?

I understand how to access an individual object in this array - objects[0], e.g. - but I am struggling how to access all objects in one array… is it possible without creacting additional vars and without using loops etc?

It is, sure, but at this point in the curriculum you likely haven’t learned about them. You’re still working with an array, so you’ll still need an array method that examines each thing in that array. Whether you loop, or you use a method that implicitly loops behind the scenes, there will still be some kind of looping.

For this, you’re expected to use a for loop. But later, you might try Array.find().

1 Like

thank you! i already passed the course and i am just repeating the assignements with which i had more strugle. i like ternary operators thats why i tried to use them.

thats what i have now:

function lookUpProfile(name, prop) {

// Only change code below this line

for (let i = 0; i < contacts.length; i++) {

return (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) ? contacts[i][prop]

: (name !== contacts[i].firstName) ? ‘No such contact’

: ‘No such property’;

}

// Only change code above this line

};

console.log(lookUpProfile(“Akira”, “likes”));

please explain me my mistakes)

So inside that loop, the return statement is returning something on the very first iteration of the loop. What happens to any iteration after that, as we’ve already returned from the function?

2 Likes

hey mate!

i am back!) I guess it returns the value of the property (in this case ‘likes’, so it returns the array [“Pizza”, “Coding”, “Brownie Points”].

as i understand, after that, iteration stops… and the loop does not go further.

now I am trying to understand what it would mean for me)) either I can alter my ternary operator, or it is just not suitable for this case and I need to use another tool.

but thanks for helping me out with this!)

p.s.
ok, i guess if we use analogy with nested if statements, there should be a similar tool for the ternary operator (which you rightly pointed in your first post , I haven’t learned about yet), so i will try to figure this out.