Profile Lookup Excercise

Tell us what’s happening:
My code is working for all tests except “bob”, “potato”.
It works for “bob”, “number”.

These two tests look very close (if not identical) in terms of what’s being tested.

I’m mystified, and have shuddered to a complete halt!

All help gratefully received

PeterL

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) && (contacts[i].hasOwnProperty(prop))) {
      return contacts[i][prop]; 
}  
else if (contacts[i].hasOwnProperty(prop) == false) {
	      return "No such property"; 
}
}
return "No such contact";
// Only change code above this line
}

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/80.0.3987.149 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

Thank you, Randall.

I was thinking that, with the first if-conditions not met, the “return” would not take effect, and the subsequent (else) statements would come into play.

I’ll set up a variable “value” and have the code assign the result to it, then finish with “return value” . Will try that now.

Thanks, PeterL

Hi Randell

Firstly my apologies for spelling your name wrongly in my reply! (I think I’m going slightly blind after my efforts here.

Yes, I see you’re right about the “else if” clause returning “No such property” prematurely. Regarding how to put it right (preferably without a near-total rewrite), I 'm now sitting staring at it, in the hope of a sudden flash of insight, or divine intervention, or equivalent.

Thanks for the help there. Wish me luck!

PeterL

let’s try with a question…
do you always need to check if the object contains the property? or only after you check if something else is true?

1 Like

Hi ieahlean,

I’ve given up trying to hang on to my beloved if-clause with the && operator. Am now setting up the nested if-clauses as recommended in the “hints” section.
I think this is the direction of travel you’re suggesting here?

As a working method, I always try to come up with my own first attempts at code before searching for any hints or help. That way I can get as much “training” out of each exercise, in terms of understanding the logic of various iterations, before looking at the experts’ ideas.

It’s also an excellent way of reducing oneself to a pile of desperate, defeated exhaustion. At the moment I’m failing to follow the logic of FCC’s first suggested solution. But I’ll keep staring at it until light dawns. This method works by way of forcing oneself to understand the logic. It’s just a bit painful, that’s all.

Many thanks, PeterL

you could keep it, you just need to do the same for the if else condition

anyway, don’t just stare at something
try to understand it, play with it…

Thanks for the suggestions, ieahleen, The work continues…

remember you can always post again a new version of your code, or all the questions you have
it’s not bad needing help - it is a good skill to know when you need to ask for help

Hi ieahleen,

Just passed the tests, following the guidance about nested if-clauses. But your earlier suggestion that I could keep my original if-clause (with the &&) then alter the subsequent code is VERY intriguing!

I’ve been working on this most of the day (between a couple of other tasks). My staring at bits of code isn’t a passive hanging-around for inspiration, but rather a concentrated attempt to understand the flow of logic. I feel if I can get under the skin of “computer-think” I may yet learn to be a half-decent coder.

All of this concentration has left me pretty much brain-wiped for now (21.15 UK GMT) but I’ll return to the same exercise tomorrow and have a go at updating my original code to see whether I can come up with the goods.

Many thanks for your help, PeterL

my suggestion is:
take pen and paper,
write down what happens to variables and other values (what checks, what changes etc) for each line of code

instead of staring, digesting like this may give you a different insight

this will work both for understanding a working piece of code, or finding bugs and discrepancies in code you are trying to make work

HI ieahleen,

Can I believe it? A modern coding-savvy woman of the modern digital age telling me (old codger who used a fountain pen for his first public exams as a schoolboy) to try a bit of pen-and-paper work!! You’ve got me grinning ear to ear.

Yes. A fountain pen. I kid you not. And a classic biro to scribble out my finals at university. I thought these days I’d get “down with the kids” and forget all that.

I was so intrigued by your earlier suggestion about keeping my original code that, shattered or not, I’ve just taken another go at it. And it worked!! Used an else-if clause with two conditions linked by &&, (as I think you were suggesting) and bingo. Problem solved twice over.

Your suggestions have been VERY interesting and helpful.

Do accept my most serious thank-you. With best wishes,

PeterL

ohi! I am a chemist first and foremost, and you will never find a chemist that will not go for pen and paper when needing to scribble down structures and equations

(I mean, I just bought an LCD tablet to avoid throwing out so much paper used just for repetition of stuff when studying…)

A chemist! I take my hat off, ieahleen. (That is…if I had a hat, I’d take it off).

In those schooldays to which I referred I went to a fairly demanding school ( a “grammar school”) and contrived to be top of the class in both physics and chemistry.

I understood and had a real liking for physics at that level (GCE O-level, as was), but remained utterly baffled by chemistry for which I felt no interest or connection.

In later years a good many scientists have told me that they tend to like either one or the other, but not both.

Back in those times, unsure of my abilities in maths, I dodged the whole issue and studied languages thereafter.

Given that you’re a full-time scientist used to the kind of logical iterative thinking which coding seems to need, I very much appreciate your suggestions and I’ll take them up and see how it works for me.

Much appreciated, ieahleen, many thanks, PeterL

1 Like

it works for me, it may not work for you (you may be more interested in rubber duck debugging maybe)

I am still studying in both fields, I am not any kind of expert
I can’t say I don’t like physics (how much quantum mechanics in my courses… :upside_down_face:), it is one of the core subjects for studying materials (classical
physics this time) (science of materials is its own field, and deserve respect)

Don’t laugh! I had to look up “rubber duck debugging”. (Perhaps a young person these days would need to look up “fountain pen”).

Having worked as a teacher, and having explained things to others from time to time, I had noticed how such explaining could sometimes improve my own understanding of whatever the matter was. I can see it being a very effective method (that I’d so far never heard of).

You’ve done it again! Another excellent suggestion.

Materials, yes, a real game-changer of a subject (I’m thinking graphene rendered as ultra-thin sheets, etc.). The right discovery at the right time can revolutionise society in one hit. Stay with it, it’s a good one.

My thanks yet again, ieahleen. Have a good day. PeterL

not anyone know about it, I would say it’s normal to have to search it!

I would usually link one of the articles about it, this time I didn’t…