Basic JavaScript - Profile Lookup

Tell us what’s happening:
Why is this incorrect? I checked the solution and it seems that I’m not supposed to use the last else {return “No such contact”}. Instead I should put a return statement after the last curly bracket from the for loop. I don’t get why. I thought with if (contacts[i].firstName === name) {} I should use an else for when my condition is false??

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

for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][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:106.0) Gecko/20100101 Firefox/106.0

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

A return statement immediately stops a function, even in the middle of a loop.

Following the given algorithm, you must return “No such contact” right after the “for” loop.

It is great that you solved the challenge, but instead of posting your working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

But a few lines before I’m already using a return, and I tested that part of the function and it did work. Does that mean, you’re not allowed to use return in general when using a loop?

That means it’s necessary to put return always at the end of the for loop?

No. You can use a return statement inside of the loop when you want the function to stop immediately.

Step through the consequences of stopping immediately. Do we want to stop immediately when we find a matching contact? Do we want to stop immediately when we find any contact that doesn’t match?

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