Basic JavaScript - Profile Lookup

Hi, I’m just wondering if I was close to making this code work without the for loop that the solution has. This code doesn’t run- but is there a small tweak to it that would make it run or does it absolutely need the for loop? This probably isn’t a very efficient post- but I am struggling hard right now, questioning if I am smart enough for programming, and right now I think it would help to know this. Thank you for reading!

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
if (name === contacts[0].firstName || name === contacts[1].firstName || name === contacts[2].firstName || name === contacts[3].firstName) {
  if (prop === firstName || prop === lastName || prop === number || prop === likes) {
    return contacts[name][prop];
  } else {
    return "No such property";
  } else {
    return "No such contact"
  // Only change code above this line

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/117.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Let’s look at the this requirement:

“If name does not correspond to any contacts then return the string No such contact.”

How will you know if this is true if you don’t go through all of the contacts?

The way you are trying to do it might work for this specific contacts object because you know exactly how many entries are in it. But what if the tests used a different object with a different amount of entries. The function should be written generically so that they can handle any contacts object passed into it, not just the one provided in the starter code.


Good Question, the simplest answer is that you are not going to see to many conditional statements, if, else, and if else with out a function and a for loop to start.

1 Like

Yeah… that makes complete sense. Thank you for your answer!

@bbsmooth That’s a good question.

@adamleikness are you asking if there is another way to iterate over the values using a function to produce the same output results?

Ah ok, ya I’m finding myself looking for the -easy way out- currently. Anyway, thank you.

I now realize that the object itself isn’t being passed into the function, and the instructions are asking you to consider just the contacts object defined, so I understand why you wrote your function like you did. So I sort of jumped the gun a little on my explanation.

But ya, as you will learn, you want to write functions that are generic enough to be able to handle all sorts of inputs.

1 Like

I was just asking if my code was very close to being able to run- perhaps if it’s just missing a couple words or symbols. I thought what I wrote would cover everything. But the earlier guy made a good point with that while he sees my logic and code could be written this way since I know the specific amount of contacts, it’s not the good way to do it, since I should be learning how to write generic code that could work for a wide/infinite range of contacts/situations.

Based on the code you presented it looks like you are trying to ‘hard code’ the answer. If you modified your code this could work but hard coding your logic is only accounting for just three elements, each element in the array happens to be an object . What if there were 100 or more elements? How would the code handle this? I agree with @bbsmooth in making the function generic. You can handle more use cases this way.

1 Like

Right, makes complete sense. I need to not think in the ‘hard coding’ mindset. At this point, I’ve dissected the correct solution and understand it completely, except for the last return statement, and will probably make a post on that if I don’t figure it out soon.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.