Search for a profile at a freecode camp checkpoint

So, I go through freecodecamp and I solve problems there to keep in a loop with programming, and I came across a trap, and I'm not quite sure what happened.

So, I have an array of objects called contacts, and I need to create a function called lookUp(firstName, prop). The text of the task is as follows:

The function should check if the firstNameactual contact is firstName, and this property ( prop) is a property of this contact.

If both values ​​are true, return the "value" of this property.

If it firstNamedoes not match any contacts, then return "No such contact"

If it propdoes not match any valid properties, return "No such property"

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": ["Intruiging Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    },
];


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

// Change these values to test your function
lookUp("Kristian", "lastName");

So, I go through an array with a loop forchecking each object. In the first, ifI check if the property of firstNamethis object is equal to the parameter of the firstName function, then if it is true, I check if the object has a property prop, and I should be able to return it. But it seems that

return contacts[i].prop;

not working and i'm a little lost why. I'm sure this is something trivial, but I don’t understand why. When I go to the console and test

contacts[0].likes

I exit the array ["Pizza", "Coding", "Brownie Points"], but in my case, if this does not work. What am I doing wrong here?

EDIT

Ok so i tried

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

But I still get the same error: \

+4
8

, , for:

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

.

+9

, , . return "No such contact". . , , console.log https://jsfiddle.net/oegw3a4y/

false if else.

+3
function lookUp(firstName, prop){
  var ans="";
  for(var i=0;i<contacts.length;i++){

//if firstname matches,fetch the property
if(contacts[i].firstName==firstName)
  {
    //in the item matched with first name check if the property is there or not.
    if(contacts[i].hasOwnProperty(prop))
    {
       ans=contacts[i][prop];
    }
    else{
      ans="No such property";
    }

  }
  }

  // if no contact found after searching entire collection.then ans will be empty
  if(ans==="") ans="No such contact";  
  return ans;
}
+3
function lookUp( firstName, prop ){
  for( var i = 0; i < contacts.length; i++ ){
    if( contacts[i].firstName == firstName ) {
      if( contacts[i].hasOwnProperty( prop ) ) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    } else {
      return "No such contact";
    } 
  }
}

'else' . firstName . . , 'else' 'if'

0

. , " ", ! :

//Setup
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(firstName, prop){
// Only change code below this line
  for ( var i = 0; i <= 3; i++ ) {
    if (contacts[i].firstName == firstName) {
      if ( contacts[i].hasOwnProperty(prop) === true) {
        return contacts[i][prop];
        } 
        return "No such property";

    }    
  }
  return "No such contact";
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Akira", "likes");
0
source

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(firstName, prop){
   for(var i=0;i<contacts.length;i++){
       if((contacts[i].firstName===firstName)&&
         (contacts[i].hasOwnProperty(prop)===true)){
             return contacts[i][prop];
       }else if(contacts[i].hasOwnProperty(prop)===false){
          return "No such property" ;
       } 
   }
       return "No such contact" ;
}

lookUpProfile("Akira", "likes");
Run code
0
source
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if (firstName === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  }
}

for (var j = 0; j < contacts.length; j++) {
  if (firstName === contacts[j].firstName && contacts[j].hasOwnProperty(prop) === false) {
    return 'No such property';
  }
}
    return 'No such contact';
// Only change code above this line
}
0
source

Here I used only the tools that we have been given so far in the FCC.

var o = "";

function lookUp(firstName, prop) {
for (i = 0; i <= contacts.length - 1; i++) {
       if (firstName === contacts[i].firstName && (contacts[i][prop])) {
           return (contacts[i][prop]);
   }
   }
// if property look up returns undefined type then there is no such property
        if ((contacts[0][prop]) === undefined) {
         o = "No such property";
     } else {
         o = "No such contact";
     }   
    return o;   
}
-1
source

Source: https://habr.com/ru/post/1624338/


All Articles