Basic javascrip Profile Lookup

Basic javascrip Profile Lookup
0

#1

Tell us what’s happening:
I don’t understand why my code isn’t working. I mean, I don’t think it’s right but I’ve tweaked so many things so far that I have no idea if I’m even in the ball park. I’ve read a lot of the forums where people are comparing firstName, which I don’t understand since you also need to return a last name. I’m super lost and have been working on this for 2 days, plz help

Your code so far


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

  
// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/


#2

Your for loop only makes one iteration, because you return a value during the first iteration. Once a return statement is executed, the function immediately exits and does not come back to finish a loop.

Hint: Think about when you should really return “No such contact”. It should not be in the first iteration or any iteration of the for loop.


#3

Ok I don’t understand.

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

#4

Did you mean to put an if or else if at the beginning of this line?


#5

My bad. Even with the “if” it’s still running the test as "“Bob”, “potato” should return “No such contact”


#6
function lookUpProfile(name, prop){
// Only change code below this line
var value;
  for (var i = 0; i < contacts.length; i++) {
  if ((name === contacts[i]["firstName"]) && (contacts[i].hasOwnProperty(prop) === true)) {
    value = contacts[i][prop];
    break;
  }
    else if (contacts[i].hasOwnProperty(name) === false) {
      value = "No such contact";
    }
    else (contacts[i].hasOwnProperty(prop) === false){
      value = "No such property";
    }
}
  return value;
// Only change code above this line
}

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

I’m just obviously not getting this at all


#7

That is because the following if statement condition is evaluates to true and you assign “No such contact” to value.

if (contacts[i].hasOwnProperty(name) === false) {
  value = 'No such contact';
}
Then, the next if statement (seen below) condition also evaluates to true and you assign "No such property" to value.

if (contacts[i].hasOwnProperty(prop) === false) {
value = ‘No such property’;
}
This happens 4 times (over the 4 iterations), so since value is equal to “No such property” in the final iteration, that is what you return.


#8

I keep reading this over and over and I don’t understand what you mean


#9

See the following repl.it page where I put some console.log statements in a various places, so you can see what is being evaluated and when assignments to value are happening.


#11

Sorry about that. There was some kind of technical difficulty that has been resolved. Try again.


#12

It showed up. I still don’t understand at all :grimacing:


#13

Can you be more specific about which part you do not understand? It might be easier to see if you click the Open In repl.it link in the upper right hand corner above the code section. It will allow you to see all the code and the console better.


#14

yeah, i mean i went to the page…
I feel like i need to assign “No such property” to something other than the variable value but IDK how to do that. is that right?
If that’s not right then I have no idea what to do even a little bit.


#15

When all else fails, write out an algorithm in plain language first and then translate the algorithm to code.

#1) Start with the first element of the contacts array

#2) Check if the current object has a firstName property equal to the argument name. If it does, proceed to step #3, otherwise move to the next element and repeat this same step again. If there are no more elements in the contacts array to check, then we know that there is no such contact with a firstName equal to name, so we return “No such contact”.

#3) Check if the current object has a property with the same name as prop. If yes, then return the value of prop for the object, otherwise return “No such property”.

That is the basic algorithm. See if you can convert it to JavaScript now.


#16

I got nothing, my guy

var value;
  for (var i = 0; i < contacts.length; i++) {
  if ((name === contacts[i]["firstName"]) && (contacts[i].hasOwnProperty(prop) === true)) {
    value = contacts[i][prop];
    break;
  } else {
      return "No such property";
    } 

  return "No such contact";
}
    }
  return value;

I’m really lost


#17

Can you please edit your last post to reflect the entire function? I can not tell if you have an extra } in the code that should not be there without seeing the entire function. You can leave off the contacts array.


#18

With your latest code, if the very first object’s firstName does not match name then your else is going to return 'No such property". What if the 2nd object’s firstName matches name? Your code will never get that far, because it already exited in the first iteration of the for loop.


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

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

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

#20

why is this in the get a hint page?

for (var x = 0; x < contacts.length; x++){
    if (contacts<a href='https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:"' target='_blank' rel='nofollow'>x].firstName === firstName) {
        if (contacts[x].hasOwnProperty(prop)) {
            return contacts[x][prop];
        } else {
            return "No such property";
        }
    }
}
return "No such contact";

telling us to use return?


#21

Your latest code (above your last reply) has an extra } on the line before return “No such contact”; which is not allowing the code to execute properly. If you fix that, you are still not following the written algorithm I showed you earlier. Do not try to combine the following two conditions in the same if statement:

		if (
			name === contacts[i]['firstName'] &&
			contacts[i].hasOwnProperty(prop) === true
		)

Also, based on the algorithm I gave you earlier, when should you be returning “No such contact”?