Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
El desafío de búsqueda de perfil de Javascript

He estado atrapado aquí por un tiempo y verifiqué otras soluciones que coincidían perfectamente con la mía. Pero parece que no puedo encontrar el error en mi código, ya que sigue diciendo ...

 // 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

Este es el reto...

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

Mi código...

 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");

Los parámetros de prueba

 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

Por favor, me alegraría mucho si alguien pudiera ayudarme a abrir los ojos.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Estaba finalizando prematuramente su función si no encontró el contacto en la primera iteración. Estuvo cerca de hacerlo funcionar:

 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 Report

0

Le recomiendo que use Array.prototype.find() , el encadenamiento opcional (?.) y el operador de fusión nula (??) .

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!