Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda