Inside my brain while building an Address Book

I’ve been struggling a bit with the Building an Address Book module on Code Academy so I’ve decided to try out a technique I’ve seen…

I’ve been struggling a bit with the Building an Address Book module on Code Academy so I’ve decided to try out a technique I’ve seen a number of code newbies and teachers recommend: writing it out.

 

The basics of the module are simple enough. Write code that will allow you to create objects “contacts”. These “contact” objects should store names and contact information for each person in your address book. Then write code that will allow you to recall the information from inside the objects.

Step one was creating an object named mary which contains an array of her information. That was easy enough though the first time I ran through the activity I created her in the var mary = new Object() syntax. The original object already there “bob” was created as an array from the start so var bob = {*info here*}; which meant slight differences between the two. I am wondering now if perhaps that was part of why I was having a hard time with the exercise? The differences between the two meant mary was accessed a bit differently than bob? I’m not sure but as I’m running through the activity again I’ve created mary in the same way bob was created so we’ll see how it all plays out.

Next up I’ve created a function to access the information inside the person objects which for now only displays the first and last name as contained within the object being called. And this brings us to where I got stuck.

The next step is to create a loop that displays all of the contacts. In order to end the loop, we’ll need a variable that stores the length of the contact list: contactsLength, to get this we’ll access the length using .length appended to our contacts array contacts.length

 

We’ll also need the for loop, which is where I ran into trouble. I need an initialization, an end point and an action to loop and print all the contacts until the end of the list. We’ve created a variable list which equals the total length of the contacts list so perhaps my initializations should be i = 0 and my end point should be i < list Lets try that and see. Ok, I’ve tried that and got an error indicating that I need to call the list function? So I added the call: list(); but I’m still getting the error. I’m not sure why though because all that the function does is define the variable. Unless the for loop is supposed to go in the function?? Lets try that.

OK, I’ve tried that and I’m still getting the error so now I’m going to take a look at the code academy forums to see if there’s anything I’ve missed. Ah, I see, It looks like because the for loop is now inside the list function I need to redo my end point parameter so instead of being i < list it would be i < contactsLength

Ok, I’ve switched that but I’m still getting the did you call the function error, however I also got undefined printed in the console this time so that seems to be a step in the right direction. I reviewed my code and noticed that in my for loop I was calling printPerson[i]; and I suspect it should actually be printPerson(i); so I switched that and I still get the call list(); error but now it prints: undefined

undefined undefined

to the console. Ah, in reviewing the forums a bit more I see what I did. I used console.log(printPerson[i]) and console.log(printPerson(i)) but including console.log in

both actually make them redundant because printPerson already includes a console.log command. I should have used printPerson(contacts[i]);

I figured this out by commenting out my code and pasting in a sample code from the forums which the poster indicated worked for them. Then I compared my code to theirs line by line. As soon as I got to that line and saw printPerson(contacts[i]); I realized my error. I also realized I was right the first time in using brackets instead of parentheses. I deleted the copied code and updated my code to fix the error. Hurray! It works now!

 

The next section is to write code that will allow us to find a specific person in our list using linear search. This requires another function containing a for loop with the same parameters as the previous but instead of printing all we create an if statement to limit to only print if the parameter passed when calling the function is the same as the contacts lastName piece. We do this by access the contacts last name with the dot method: contacts[i].lastName

The final section was to write a function which allowed you to add a new contact which took the parameters of the contact entries: firstName, lastName, email, & phoneNumber. This was actually probably the easiest part. The directions seemed to be the most straightforward of this project so I just followed along and everything worked on the first try. I did notice that they listed the parameters in a different order than the prior entries had them in so I was a bit worried about that but I did them in the order listed in the instructions and it worked just fine. I’m thinking because the name stays the same regardless of what order they are added in.

 

Anyway, that was a peek into how I worked through the build an address book lesson on Code Academy. I really enjoyed writing my thought process out, it definitely seemed to help so you’ll probably see more entries like this in the future.

 

p.s.- feel free to check out my code academy profile here.