Build a Profile Lookup - Build a Profile Lookup

Tell us what’s happening:

why if statement is not working even when the values in function are correct.

Your code so far

let 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){
  for(let i = 0; i <= contacts.length; i++){
    if(contacts[i].firstName === name){
      console.log(contacts[i])
      return contacts[i][prop];
    } else {
      console.log(contacts[i]);
      return "No such contact";
    }
  }
}

console.log(lookUpProfile("Kristiam", "lastName"))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Profile Lookup - Build a Profile Lookup

let’s consider what happens

when i is 0 the program checks the first object

in here if(contacts[i].firstName === name){ is false, so the else is executed, so return "No such contact"; is run, and your function stops without looking at the rest of the array

you can use a tool like https://pythontutor.com/ to see what happens in your code line by line

Thanks for this tool. I’m gonna try and see, i’ll come here again if don’t get it

I’ve checked, it only works good for first object. you’re right on first step if it is false it goes in else block and function stop. so maybe i should remove the else block?

i have changed the position of code now it works fine for all object but only for correct one, if we put wrong input to display “no such contact“ it doesn’t work and says cannot read the properties undefined

let 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){
  for(let i = 0; i <= contacts.length; i++){
    if(contacts[i].firstName !== name){
      console.log(contacts[i])
      // continue;
      // return contacts[i][prop];
    } else {
      console.log(contacts[i]);
      return contacts[i][prop];
    }
  }
}

console.log(lookUpProfile("Sherlockee", "lastName"))

for the error TypeError: Cannot read properties of undefined (reading 'firstName')

what happens when i is equal to contacts.length?
what element is there at contancts[contacts.length]?

loop goes for 0 to 3, but contacts.length is 4. there isn’t any object at 4

maybe i should do something like i < contacts.length ?

do you still get the error “Cannot read properties of undefined” if you do that change?

not, but now for the wrong input it says undefined.

let 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){
  for(let i = 0; i < contacts.length; i++){
    if(contacts[i].firstName !== name){
      console.log(contacts[i])
      // continue;
      // return contacts[i][prop];
    } else {
      console.log(contacts[i]);
      return contacts[i][prop];
    }
  }
}

console.log(lookUpProfile("Sherlockee", "lastName"))

so if the name does not match, you return contacts[i][prop]?

you may want to get pen and paper and plan out the steps of the algorithm carefully before writing code, having a logic plan already written is a good starting point before writing code

figure out when to return the proeprty value, when to return "No such property", when to return "No such contact"

no, it was for testing purpose. the actual logic will be:

if(name match){
    return contacts[i][prop];
    if(no property match){
        retrin "no such property"
    }
} else {
    return "no such contact"
}

are you aware that the if after a return is never going to be executed?

and if name not match on first contact you are returning no such contact, what about checking the other contacts?

yeah, i was wrong here. can you help me how can i do it in better way. I’m stuck in this problem from 4 days i don’t want to use AI i want to do it myself

do not think about code for a moment

you have a book of contacts, there is a contact per page, you need to find Sherlock, and find out what’s his number.

How would you do it?
Like, open the first, page, there is Akira, the name is wrong, so now what do you do?

let’s say we have to find Sherlock:
open the first page, there is Akira, skip it and go to the next page. if there is Sherlock, get it.
else, again go to the next page.
continue this process until name === Sherlock

how would you write the code for this?

actually, that’s where i’m stuck.

do not think of other complications, write only the code that when the correct name is found, returns the property

how do you check if the name is the correct one?

function lookUpProfile(name, prop){
  for(let i = 0; i <= contacts.length; i++){
    if(contacts[i].firstName === name){
      return contacts[i][prop];
    }
  }
}

are you sure you want to also check contacts[contacts.length]? is there something there?


ok, now, sometimes you find the name, but the property is not there, so you need to adapt the code so that you do not return undefined, but return No such property, there are many ways to do it, as long as you find one that works that’s fine