Basic JavaScript - Profile Lookup

In profile lookup Exercise , I hands on this exercise and I write that code given below.

function lookUpProfile(name, prop) {
  // Only change code below this line
for(let i = 0; i < contacts.length; i++) {
if(contacts[i].firstName === name) {
  if(contacts[i].hasOwnProperty(prop)) { 
  return contacts[i][prop];
  }
  return "No such a Property";
}
}
return "No such a contact";

Run this code it shows error in if it is bob, it will return the No Such a contacts and Still I get confuse why it shows an error because, I write if it has name and property it will return that property of the name inside the contacts(array object) else, it will return “No such a property”.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

please post a link to the challenge you are on.

can you also double check and post all the code? Right now your code is missing the } that closes out the function.

also here, the text No such a Property and No such a contact is incorrect.
You must use the text provided in the challenge. (I can’t tell you what that is because I don’t have a link to the challenge to check at the moment)

this is the link to that challenge: Basic JavaScript: Profile Lookup | freeCodeCamp.org

thanks. I confirmed that if you fix the two sentences I mentioned earlier, your code will pass.

I checked this thread once I managed to get to a solution myself and it seems that I overcomplicated things a bit - I’m just wondering how much of an overkill this is, would this be considered bad code?

function lookUpProfile(name, prop) {

	let index = contacts.length - 1; // ?
	let noName = 0;
	
	while (index >= 0) {
		
		if (name === contacts[index]["firstName"] && contacts[index].hasOwnProperty(prop)) {
			let lookupResult = contacts[index][prop];
			return lookupResult;
		} else if (name === contacts[index]["firstName"] 
			&& contacts[index].hasOwnProperty(prop) === false) {
			return "No such property";
		} else if (name !== contacts[index]["firstName"]) {
			noName++;
	
	index--;
	}
	
	if (noName === contacts.length) {
		return "No such contact";
	}

}

lookUpProfile("Akira", "likes");

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.