Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
The Javascript Profile Lookup challenge

I've been stuck here for a while, and I checked on other solutions which matched mine perfectly. But I can't seem to find the error in my code as it keeps saying..

// running tests
lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
// tests completed

This is the challenge...

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"],
  },
];

My code...


function lookUpProfile(name,prop){

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

  }
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

The test parameters

lookUpProfile("Kristian", "lastName") should return the string Vos

lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]

lookUpProfile("Harry", "likes") should return an array

lookUpProfile("Bob", "number") should return the string No such contact

lookUpProfile("Bob", "potato") should return the string No such contact

lookUpProfile("Akira", "address") should return the string No such property

Please I'd be so glad if anyone can help open my eyes.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You were prematurely ending your function if you didn't find the contact in the first iteration. You were close to getting it to work:

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) {

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

  // Only change code above this line
}


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

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

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

console.log( lookUpProfile("Bob", "number") );

console.log( lookUpProfile("Bob", "potato") );

console.log( lookUpProfile("Akira", "address") );

about 4 years ago · Juan Pablo Isaza Relatório

0

I'd recommend that you use Array.prototype.find(), optional chaining (?.) and the nullish coalescing operator (??).

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) {
  const profile = contacts?.find(contact => contact.firstName === name);
  return profile?.[prop] ?? "No such property";
}

const results = [
  lookUpProfile("Kristian", "lastName"), // should return the string Vos
  lookUpProfile("Sherlock", "likes"), // should return ["Intriguing Cases", "Violin"]
  lookUpProfile("Harry", "likes"), // should return an array
  lookUpProfile("Bob", "number"), // should return the string No such contact
  lookUpProfile("Bob", "potato"), // should return the string No such contact
  lookUpProfile("Akira", "address"), // should return the string No such property
];

results.forEach(result => console.log(result));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda