freeCodeCamp - Profile Lookup beta- issue with Bob and potato [SOLVED][SPOILER]

freeCodeCamp - Profile Lookup beta- issue with Bob and potato [SOLVED][SPOILER]
0

#1

Hello friends !
I’m doing the Profile Lookup from the beta (link: https://beta.freecodecamp.org/en/challenges/basic-javascript/profile-lookup) but i’m having issue regarding this rule:
“Bob”, “potato” should return “No such contact”

English is not my native language but i think there is an error in the ask !
If potato is a property and it doesn’t exist, why does it have to give back “No such contact” ?

Here my code:

var 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) {	
	//little 'cheat' to pass the exercice
	/*
	if( (name == "Bob") && (prop == "potato") ) {
		// return "No such contact";
	}*/
	var a, b = false;
	
	for(var i = 0; i < contacts.length; i++) {
		if( contacts[i].hasOwnProperty( prop ) ) {
			a = true;
			if( contacts[i]["firstName"] == name ) {
				b = true;
				return contacts[i][prop];	
			
			}else {
				b = false;
			}
			
		}else {
			a = false;
		}
	}
	
	if( a == false ) {
		return "No such property";
	}

	if( b == false ) {
		return "No such contact";
	}
};


console.log( lookUpProfile("Kristian", "lastName") ); //"Vos"
console.log( lookUpProfile("Sherlock", "likes") ); //["Intriguing Cases", "Violin"]
console.log( lookUpProfile("Harry", "likes") ); // an Array
console.log( lookUpProfile("Bob", "number") ); //"No such contact"
console.log( lookUpProfile("Bob", "potato") ); //Should be this from the rule "No such contact" but is "No such property" by logic
console.log( lookUpProfile("Akira", "address") ); //"No such property"

Maybe i don’t fully understand this sentence:
If prop does not correspond to any valid properties of a contact found to match name then return “No such property”

Please, let my understand why i may be wrong and explain it to me. Or please confirm my thought so i can move on with the peace of the mind :slight_smile:

Have a good life !


#2

You need to modify your logic, so that you are first checking if the firstName exists. In your current code, you are checking whether prop exists.


#3

Hello friend !
Thanks to you and my relecture of the rule, i figured it out:

function lookUpProfile(name, prop) {	
	for(var 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 ) == false) ) {
			return "No such property";
		}
	}
	
	return "No such contact";
};

console.log( lookUpProfile("Kristian", "lastName") ); //"Vos"
console.log( lookUpProfile("Sherlock", "likes") ); //["Intriguing Cases", "Violin"]
console.log( lookUpProfile("Harry", "likes") ); // ["Hogwarts", "Magic", "Hagrid"]
console.log( lookUpProfile("Bob", "number") ); //"No such contact"
console.log( lookUpProfile("Bob", "potato") ); //"No such contact"
console.log( lookUpProfile("Akira", "address") ); //"No such property"

It’s far more simple than what i did before.

If the firstName is good and the prop exist, you return the information.
If the firstName exist but the prop not, you return “No such property”.
And if you finished the for loop without completing these two conditions, it mean that the firstName and the prop doesn’t exist both.

The exercice result in two explicit conditions and one implicit condition.

Have a good life !


#4

Glad you figured it out. Now that you have solved it, I am going to show you a similar way to solve without repeated code. What I mean by repeated code is the way your if and else if conditions both have:

(contacts[i]["firstName"] == name)

So here is a DRY (don’t repeat yourself) approach. The else if is not needed in this solution. Click on the blur to see the code.

function lookUpProfile(name, prop) {	
  for(var i = 0; i < contacts.length; i++) {
    if( contacts[i]["firstName"] === name) {
      if (contacts[i].hasOwnProperty( prop ) === true) {
        return contacts[i][prop];
      }
     return "No such property";
    }
  }
  return "No such contact";
};

#5

Hello ! Thanks you .
That’s a good way to do it ! One ‘if’ because it’s the same for both and the second ‘if’ is here to split in two.
I will try to copy this tips.

Have a good day !