Basic js , profile lookup

hi guys, i need your help !
i’m failing the first 3 tests :
Failed: lookUpProfile("Kristian", "lastName") should return the string Vos
Failed: lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
Failed: lookUpProfile("Harry", "likes") should return an array

can some one tell me what am i doing wrong here , i believe the problem is either in the part at which the function returns the prop’s value or that i’m sending the return value of the wrong i ( for example sending i=0’s return value for the 2nd object )

function lookUpProfile(name, prop) {

for (let i = 0 ; i<contacts.length ; i++  ){
  if(contacts[i].firstName === name) {
      if (prop in contacts[i]) {return contacts[i][prop];}
      else {return "No such property";}
else if (contacts[i].firstName !== name)  {return "No such contact"}

now that i have spent multiple hours looking at my code and the solution code i can phrase the problem in a better way and i actually found a solution that scratch my itch !

please look the the 2 solutions that we can get when we press “Get a hint”

the problem here is that the function will return no such contact regardless of the for loop outcome , it’s outside the for loop and thus i believe we will get unneeded no such contacts when the name and/or the prop are found
for example:
lookUpProfile(“Harry”, “likes”) will return an array + no such contact after the for loop gets executed … the test is passed but logically there is a problem

since return"no such contact" can’t be put in the for loop without being executed at every contacts[i] and we shouldn’t put it outside the loop as it will be executed regardless of the for loop outcome

i collected first-names from objects and pushed them into an aray and checked if the name is included in that array in another part

let me know if i missed something , ty in advance

function lookUpProfile(name, prop) {

for (let i = 0 ; i<contacts.length ; i++  ){
if (contacts[i].firstName===name){
  if (prop in contacts[i]) {return contacts[i][prop];}
  else if (!(prop in contacts[i])) {return"No such property"}
let firstNameGroup=[];
for (let j = 0 ; j<contacts.length ; j++  ){
firstNameGroup.push(contacts[j].firstName) ;
return firstNameGroup.includes(name)? null : "No such contact" ;

Are you saying that the posted solutions are incorrect? I’m sorry but I think you’re misunderstanding some of the code.

The solutions put it outside the for loop, because in cases where the contact is found, they will return a value before ever reaching that line of code.

sorry for what i appreciate the feedback ,
so once a return line is executed the other return lines won’t be executed ?

i don’t disagree that one will happen before the other , the question is will the return line outside the for loop get executed after the for loop in every case ? (even when contact[i].firstName===name )

Correct, a return statement immediately ends execution of the function and returns specified value. The remaining lines of code are not queued up, they are just completely ignored.

1 Like

this is noted , what about a case in which there are multiple returns expected , would the function stop after the first return execution or will it carry out all the return statements then stop

pseudo code example :
function geometric_Sequence(n){
if( n>0) {
return n10;
return n
return n1000;
return n
return n*100000;

As I said, return immediately halts the function and all remaining lines of code are unreachable.

You can test this out yourself with the function you wrote above → Node.js Online Compiler & Interpreter - Replit

roger that , i tested the code and the output for geometric_Sequence(5); was 50

ty for your help and explanation

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