Basic JavaScript - Profile Lookup (alt solution not working)

Tell us what’s happening:
Hi all, new to programming here. Wanted to figure out why my code is not passing all the criteria. The main issue is that the ‘name’ argument is failing the .firstName check and is hence returning ‘No such contact’ however when I console log the ‘Akira’ as the ‘name’ argument it seems to work fine. It’s like it’s only working for ‘Akira’ array.

Your code so far

function lookUpProfile(name, prop) {
  // Only change code below this line
for (let i = 0 ; i < contacts.length; i++){
  if (contacts[i].firstName === name){
    if (contacts[i][prop] != undefined){
      return contacts[i][prop];
    }else{
      return "No such property";
    }
  }else{
    return "No such contact";
  }
}
  // Only change code above this line
}

// 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
for (let i = 0 ; i < contacts.length; i++){
  if (contacts[i].firstName === name){
    if (contacts[i][prop] != undefined){
      return contacts[i][prop];
    }else{
      return "No such property";
    }
  }else{
    return "No such contact";
  }
}
  // Only change code above this line
}

console.log(lookUpProfile("Harry", "likes"));

Your browser information:

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

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Try following the logic by hand for one of the failing cases.

Ie. what’s contacts[i] at each step, what happens with it, is something returned, or not?

So it’s failing at the first step itself, when you swap out ‘Kristian’ with ‘Akira’ it works fine! I am guessing because Akira is the first object in the array it does not have a problem however when it comes to looping it seems to fail.

Below code is only the first check i.e if firstName === name then return (prop) else ‘No such contact’.

function lookUpProfile(name, prop) {
  // Only change code below this line
for (let i = 0 ; i < contacts.length; i++ ){
    if (contacts[i].firstName === name){
      return contacts[i].lastName;
    }else{
      return "No such contact";
    }
  }
  // Only change code above this line
}

console.log(lookUpProfile("Kristian", "lastName"));

Okay, any ideas why it might be failing for other contacts? What happens, during the check, if the first contact doesn’t have matching name?

1 Like

It appropriately returns “No such contact”

Putting that in other words, your loop says: “if the first contact’s first name doesn’t match, immediately return “No such contact” and give up looking”.

1 Like

How can fix that? As per some other solutions I’ve explored some people use the || operator or i see they have not used else to end the if statement. What do you think is the best way I can fix this issue?

Can you say inside of the loop that looks at every single contact that absolutely zero contacts will match? When can you say that zero contacts match?

Nevermind I got it! The return statement was enclosed within the first IF statement, so as a result it was not looping , but i have a new question now . Why are we not concluding the if statement with an ‘else’, because when i put 'else ’ before return it’s giving me a syntax error. basically

function lookUpProfile(name, prop) {
  // Only change code below this line
for (var i = 0 ; i < contacts.length; i++ ){
    if (contacts[i].firstName === name){
      if (contacts[i][prop] != undefined){
        return contacts[i][prop];
      }
      else return "No such property";
    }   
  } else return "No such contact";
  // Only change code above this line
}

is not working but

function lookUpProfile(name, prop) {
  // Only change code below this line
for (var i = 0 ; i < contacts.length; i++ ){
    if (contacts[i].firstName === name){
      if (contacts[i][prop] != undefined){
        return contacts[i][prop];
      }
      else return "No such property";
    }   
  } return "No such contact";
  // Only change code above this line
}

is working

A for loop doesn’t use the else keyword

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