So it looks as though my code is iterating through each array based on my for loop, however it only recognizes whatever index number is in the for loop. What I mean is, when I change the for loop to x = 1, it recognizes Harry and all of its properties, but it gives Akira and all of the other profiles in the array “No such contact” in the console log.
I’ve tried tweaking a couple of things which didn’t do anything, but I can’t figure out for the life of me why it’s not working. I checked the video and it made sense and seems like I did it right, but the function will only work on 1 element in the array of contacts. Does anyone know why it’s not working?
Your code so far
// Setup
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) {
// Only change code below this line
for (let x = 0; x < contacts.length; x++) {
if (contacts[x].firstName === name) {
return contacts[x][prop] || "No such property";
} else return "No such contact";
}
// Only change code above this line
}
lookUpProfile("Sherlock", "likes");
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/116.0.0.0 Safari/537.36
Challenge: Basic JavaScript - Profile Lookup
Link to the challenge: