Code dose not work as intended

Tell us what’s happening:
Describe your issue in detail here.

i dont undesrtand why this code dose not work as intented
i cant find a problem help me pleas
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(let i=0;i<contacts.length;i++){
if(name===contacts[i].firstName && prop===contacts[i].hasOwnProperty(prop)){
return contacts[i][prop] ;
}
else if(name!==contacts[i].firstName){
 
 return "No such contact";
}
else if(prop!==contacts[i].hasOwnProperty(prop)){
  
  return "No such property"
  }

}
 // 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/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37

Challenge: Profile Lookup

Link to the challenge:

The return statement immediately stops the function, no matter when it is encountered.

but there is an if statement so it means if the condition is not true the code wiil not be executaded so the return statement wont run if the condition is not true , that means it wiil not stop the code

There’s going to be at least one contact for which this if statement is true.

i suggest you go
Basic JavaScript: Profile Lookup | freeCodeCamp.org
and try to do the challage and show me how its done

No. It is against the forum rules for me to write the answer for you.

I’ve done this challenge multiple times. Your problem is that you are prematurely returning from your function. Even if you are in the middle of a loop, a return statement immediately stops your function preventing any further loop iterations.

Tell us what’s happening:
Describe your issue in detail here.

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

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/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37

Challenge: Profile Lookup

Link to the challenge:

This will still interrupt your loop and return the very first time a contact that meets this condition is encountered.


What does this code do?

function returnNumber(num) {
  for (let i = 0; i <= num; i++) {
    if (i === num) {
      return num;
   } else {
      return "number not found";
   }
 }
}

console.log(returnNum(5));

have already used this and passed the challenge

function lookUpProfile(name, prop) {
  for (let x = 0; x < contacts.length; x++) {
    if (contacts[x].firstName === name) {
      if (contacts[x].hasOwnProperty(prop)) {
        return contacts[x][prop];
      } else {
        return "No such property";
      }
    }
  }
  return "No such contact";
}

and it works perfectly
i wanted to understand why is it when i use

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

it wont work but have already passed the challenge so dont hold back the knowledge fam … i want to understand so i can improve

I recommend not copying the answers. I recommend not looking at the extra solutions until you solve it yourself. Getting the answer isn’t the point. The answer is pointless. Practicing the problem solving is the point.

The problem is still the exact same thing I’ve said a few times now - a return statement immediately stops your function.

theoretically the loop will run. to a point where i=5;
the return 5;

The return statement in the else body stops the loop from running that far. Try it.

yea for sure,…now am confused i thougth the loop should ran first and check if condition is true or false the proceed to the else statement

The if-else statement is inside of the loop. The if-else runs on every single loop iteration.

so what could i do to avoid the incident in this case

function returnNumber(num) {
  for (let i = 0; i <= num; i++) {
    if (i === num) {
      return num;
   } else {
      return "number not found";
   }
 }
}

console.log(returnNum(5));

If you don’t want a particular behavior to interrupt the lop, then it has to come after the loop.

code example please.

How about you try and see if you can take the else clause out of the loop body?

On the first line inside your for loop, add this log statement

console.log(i);

You should notice that it never goes above 0. Why is that?

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