Tell us what’s happening:
Greetings everyone!
I will preface this by saying that I do know how to solve this exercise (I completed the JAvascript course a few months ago, then went on vacation for about 2 months, and I’m currently going over the entire curriculum once again).
I’m not asking for the solution here (or at least not for a very different solution), but rather, I would like to know why this proposed solution isn’t working. Even if I do know how to solve this problem, an important part of learning is understanding why a piece of code doesn’t work the way you’d like it to, so I’d like to understand this before moving forward.
Summary:
It’s the one in which you have to create a function lookUpProfile(name, prop) that will loop through a list of users to see if a user ( name ) exists and, if he does, if he has a certain property and what its value is ( prop)
Three possible returns:
- If the user exists and has said property, the function returns the value of said property.
- If the user exists but the property does not, the function returns “No such property”.
- If the user doesn’t exist, the function returns “No such contact”.
The Problem:
The only part that doesn’t work is #3: returning “No such contact” .
Here’s the reasoning behind my approach: if I’m using a for loop that stops looping and returns a result when it matches a contact’s name, that means if the loop passes by the very last contact and this wasn’t a match, then there are no matches and it should return “No such contact” . Here’s the for loop I wrote, and the relevant parts are the first and last lines, as everything inbetween works fine:
** for (let i=0; i<=contacts.length+1;i++){ **
if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {return contacts[i][prop]}
else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)==false) {return "No such property"}
**else if (i === contacts.length) {return "No such contact"}**
So the idea is that, if the i variable of the for loop ever grows to be the value of contacts.length, that means it has already cycled through all the values in contacts , didn’t find any matches, and now I want it to return “No such contact”. That’s why the last part of the code reads
else if (i === contacts.length) {return "No such contact"}
I think it sounds good on paper. Yet for some reason I cannot figure out, it does not work. And I’ve been looking at it and fidgetting with it for a while now. Again, I do know other ways to solve this problem, so I’m not asking for solutions. I’m asking cfor someone to please explain to me why this particular method doesn’t work, as it makes perfect sense to me. Is i unable to grow up to contacts-length ? Is it impossible to use variables in this way in for loops? I see no reason why i could not grow to equal contacts.length , and I even set the for loop to be for (let i=0; i<=contacts.length +1 ;i++) even though I don’t think the +1 is necessary, just to make sure i could keep growing.
Intriguing facts: if I set the last else if statement to else if (i === 3 ) {return “No such contact”}, the exercise clears without issue. I assume it has to do with the fact that there’s 4 contacts, so contacts[3] is the last position.
Mind-boggling facts that are probably unrelated : for some reason, changing console.log test codes alters the result. You can see at the end that console.log(lookUpProfile(“Bob”, “number”)) is commented out. Right now, all the Ctrl+Enter tests pass except for the ones that should return “No such contact” . However, if I ¿“de-comment”? console.log(lookUpProfile(“Bob”, “number”)) to test it on the console, ALL the fCC tests will come out negative when I press Ctrl+Enter, even though the console.log shouldn’t alter the function in any way. No idea what’s going on there.
THANK YOU in advance to anyone who might be willing to take the time to read through my post and try to help me with this!
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 i=0; i<=contacts.length+1;i++){
if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)) {return contacts[i][prop]}
else if (contacts[i].firstName == name && contacts[i].hasOwnProperty(prop)==false)
{return "No such property"}
else if (i === contacts.length) {return "No such contact"}
}
// Only change code above this line
}
console.log(contacts.length)
// console.log(lookUpProfile("Akira", "likes"));
// console.log(contacts[0].likes);
// console.log(lookUpProfile("Kristian", "likes"));
// ;console.log(lookUpProfile("Bob", "number"))
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: