Build a Profile Lookup - Build a Profile Lookup

Tell us what’s happening:

Hey, my code passes all the tests presented, except the lookUpProfile(“Sherlock”, “likes”) should return [“Intriguing Cases”, “Violin”] one, my code outputs [ ‘Intriguing Cases’, ‘Violin’ ].

How should I fix that?

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){
  let result = ""
  let resultArr
  let index = 0
  for (const contact of contacts){
    if (contact.firstName === name || contact.lastName === name) { 
      break
    } else index++; 
  };
  if (index >= 4) return "No such contact";

  if (contacts[index][prop] !== undefined && prop !== "likes") {
    result += contacts[index][prop]
  } else if (contacts[index][prop] !== undefined && prop === "likes") {
    resultArr = contacts[index][prop].splice(0);
    return resultArr
  } else return "No such property"
  
   return `${result}`
};

console.log(lookUpProfile("Sherlock", "likes"))

Your browser information:

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

Challenge Information:

Build a Profile Lookup - Build a Profile Lookup

Welcome to the forum @ragNa !

Delete or comment out your console.log() lines, when you check your code. They can mess the tests.
It´s true for every challenge, if you do extra checks, always comment it out or delete it before submitting.

why are you using a template literal here? what if the thing you are retrieving is a number? don’t you think you should return it as a number? same for arrays or other data types

Oh yeah, it was just a leftover from previous testing. But yeah it is unneeded there, normally it wasn’t a template literal

Yeah, thank you. Removing the console.log that I used to test the code worked. Now it passed all tests.

it accidentally pass the tests

call it twice

console.log(lookUpProfile("Sherlock", "likes"))
console.log(lookUpProfile("Sherlock", "likes"))

you are changing the contacts object, the second one finds an empty array []

damn okay, that makes sense, i’ll keep this information in mind for the future. ty

why don’t you try to write code that does not change the object?