Profile Lookup Challenge - Brick Wall

Profile Lookup Challenge - Brick Wall
0.0 0

#1

I’m on my 3rd go of the JavaScript section of FCC and everything mostly makes sense with the intro to arrays, objects, and For loops. But every time I get to the ‘Profile Lookup’ challenge it’s not just a gateway, it’s like a brick wall for me. For example I first tried to create a nested For loop because it has objects nested in an array so I assumed to reach the objects I’d have to nest another loop. Then when I look at hints and get to the conditional part I end up having to look that up too. Basically I wasn’t able to do any of it completely on my own without looking up the solution. Did this challenge break anyone else’s brain, or did most people know how to tackle it right away? I’m wondering if I should just throw in the towel. If I can’t do this I can’t imagine getting to the point that I could code a Simon game. Currently, I’m an IT Project Manager with a good job, but I really wanted to get into programming.


#2

If you share your solution(s) with explanations of what isn’t working and what your thought process is, then we can help nudge you in the right direction.


#3

Thanks, Ariel! I have the solution. It’s just that I had to look it up. My issue is that I couldn’t even do step 1 without looking it up. Let alone tackling the entire thing. I was just curious if that’s common, or if most people are able to get to that challenge and come close to resolving it without assistance.


#4

People hit blocks at different points. Concepts that are intuitive to some are completely alien to others. In the future, don’t hesitate to reach out to fellow campers. A lot of the time we can help you understand ‘step 1’ (or whatever step you’re blocked on) in a way that lets you finish on your own.


#5

I don’t think you’re giving yourself a fair shake if you’re ONLY using the FCC tutorials to learn this stuff. Based on your post, it sounds like you had some gaps in your conceptual knowledge of objects when you attempted this challenge. So before you “throw in the towel,” I suggest you switch up your learning technique.

  1. Like ArielLeslie said, try posting your solution attempts when you get stuck so other campers can see what you’re thinking and alert you to any oversights/gaps you might have.

  2. Maybe take a look at W3Schools.com. They have free (no signup) text-based tutorials for web development, including a Javascript module. It has a semi-similar presentation to FCC, but you never know when a slightly different perspective will get you to that “Aha!!” moment.

  3. Udemy.com (video courses) is having a $9.99 sale right now, and they have some really great instructors there. Just be sure to shop around and look at ratings, reviews, course updates, etc. They even have a few free Javascript courses that are rated at 4.5+ stars, but you have to create an account.


#6

Yes, this challenge really flummoxed me. I got so frustrated I posted an angry rant on the FCC facebook earth page about how frustrating learning js can be. I lost sleep over it. (I’ m not joking about that). The next day I kept working at it while reading the replies to my rant (there were lots, it seems a lot of people could sympathize with me). I eventually found a solution basically by sheer force of perseverance. This happened again with one of the basic algorithm scripting challenges. It took me about 2+ days to finish it, but I’d learnt from the earlier experience to just keep at it, google around, look at previous discussions (without looking at solutions) and eventually I found the solution. I’m a landscaper by trade and not very tech savvy. If I am smart enough to find these solutions you, as an IT manager certainly are.


#7

Thank you so much. This helps a lot. I may have to try to find that post. I will keep working at it. Right now I’m pasting the array in to the Chrome console and just practicing lookups and For loops on it with different console.log() outputs to see what happens. I’ll have to practice my breathing exercises before I get to the algorithms.


#8

One specific question I had was: How does the For loop access the objects while it’s in an array? I assumed you’d have to designate the array first for a lookup/manipulation (EG: contacts[0].firstName or contacts[0][firstName]). I’ll try adding another array to see what happens but in all the FCC array and objects challenges there weren’t lookups with objects nested in arrays.


#9

Can you expand on this? I’m not sure what you mean. When you are accessing objects in an array within a for loop you are looking at contacts[0].firstName and then contacts[1].firstName, etc.


#10


The courses are really nice too. :wink:


#11

I’m getting closer, just trying to fully understand the ‘reach’ of the For loop. Since the objects were in an array I was thinking the For loop wouldn’t iterate every key/value in the objects but only iterate object 0, object 1, etc. as wholes. I tried nesting arrays and printing values in the console to try to understand that ‘reach’. Thanks again for your response. If I keep experimenting I think I can get it down.

var x = [
  [
    {
      "animal":"cat",
      "lives":"9"
    },{
      "animal":"dog",
      "lives":"1"
    }
],[
    {
    "animal":"lizard",
    "lives":"3"
    }
  ]
];
for (var i=0; i<x.length; i++){
  console.log(x[0][i].animal);
}
//output:
//cat
//dog

But…

  1. If I try to console.log(x[i].animal) it comes back undefined and doesn’t reach the ‘lizard’.
  2. If I try to console.log(x[1][i].animal)) to get ‘lizard’ I’ll get the below error UNLESS I specify i<x[1].length in the For loop:

//output:
//lizard
//Uncaught TypeError: Cannot read property ‘animal’ of undefined
at :18:23

Not sure how to get all 3 animals.


#12

Hi,

I hope this helps some. If it only confuses more please disregard.

var  x = [ [obj,obj], [obj] ]
// x is array with two elements, both elements are themselves arrays

////////////////////////////////////////////////
x[0]  // is an array with two elements, both objects
      // [ {animal:"cat", lives:"9"}, {animal:"dog", lives:"1"} ]

x[0][0]  // is an object {animal:"cat", lives:"9"}
x[0][1]  // is an object {animal:"dog", lives:"1"} 

x[0][0]["animal"]  // is object property animal, value "cat"
x[0][1] ["animal"] // is object property animal, value "dog"

////////////////////////////////////////////////
x[1] // is an array with one element, an object [ {animal:"lizard", lives:"3"} ]

x[1][0]  // is an object  {animal:"lizard", lives:"3"} 

x[1][0]["animal"] // is object property animal  value "lizard"

So there is no x[i].animal
There is
x[0][0].animal
x[0][1].animal
and
x[1][0].animal

x[1][i].animal will access “lizard” only when i = 0

You may want to look into nested loops. Here’s some pseudocode to get you started

for (i < x.length){  //for each subarray in x
   for(j < x[i].length) {  //for each object in current subarray
      console.log( x[i][j].animal)
   }
}

Good luck on this. You’ll get it. I think there is an “Aha!” moment in your near future.


#13

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#14

alhazen1 gives a good explanation. I’ll leave it at that and you let us know if you need more.

I will just add some basic debugging advice. We ALL have moments in code where we try and do some object and it comes back undefined, and we all scream at the computer, “WTF! No way! It is defined! Suck my track ball!” It just sounds like you’ve got it happening too often - but that will get better as you learn. But it will never stop happening completely.

Unless I see an obvious error, my first step, always, whenever I am having a problem is to console.log the data. It looks like you started:

If I try to console.log(x[i].animal) it comes back undefined and doesn’t reach the ‘lizard’.

OK. My next step would be console.logout(x[i]). I want to see exactly what parent object is. In the browser, if it is a valid object/array. It will tell me if it is an object or array or whatever. I can see all the elements and properties and find out if ‘lizard’ isn’t there, what is. If that came back undefined or something expected, then I would walk up the tree and console.logout(x). I would get to a point where the data structure was what I expected and then work my way down again and figure out what when wrong, where.

Coders develop a “spidey sense” for finding bugs. You will too. But the way you will get it, the same as every coder everywhere, is by making a lot of stupid mistakes. A lot. But every time you struggle with these, you are building that sense. Unfortunately, by the time we get it, we need it less. Maybe that’s the only way it can be.

It’s like I tell my jazz improv students, “If you’re not making mistakes, you aren’t trying hard enough. Don’t live in your comfort zone. It’s only for vacations.” As a coding student, I would tell you that if you weren’t making mistakes, it would be because you weren’t learning anything new.

Wear your mistakes with pride. Savor every frustrating beg/error that drives you crazy.

Sorry if I’m starting to ramble (more than usual). That brownie is kicking in.


#15

So much great info here. Thanks much and to @alhazen1 as well. I do think I’m close, this just gets me back to my main question. In the Profile Lookup challenge, why don’t you need the [0] to access the array first before accessing the objects when you loop and call them?

SPOILER!

Why isn’t it always: comments[0][i].firstName vs. the correct answer: comments[i].firstName? And why doesn’t the For loop have to be For (var i=0; var < comments[0].length; i++) vs. For (var i=0; var < comments.length; i++)?

Or actually my first thought was to use the nested loop like:

 for (var i=0; i < contacts.length; i++){  //for the array in contacts
   for(var j=0; j < contacts[i].length; j++) {  //for each object in the array
      console.log( contacts[i][j].firstName)
   }
} 

#16

Not sure where the variable comments came from. I think you mean contacts, so I have will be referring to contacts from here on out.

The code above also has major syntax issues. A for loop must have an initialization expression end with a semi-colon followed by a condition expression (which you have provided in each of yours) with a semi-colon after it. A third (more optional) statement defines an expression to be evaluated at the end of each for loop iteration.

A valid for loop to display each objects’ property values would look like:

  for (var i= 0; i < contacts.length; i++){
    var contactObjectProps = Object.keys(contacts[i]);
    for(var j = 0; j < contactObjectProps.length; j++) {
      console.log(contacts[i][contactObjectProps[j]]); // displays each object's property values
    }
  }

#17

Thanks, I fixed my code. I think with more practice I’ll look back on this as being like 2+2=4. All of this info was very helpful!