6. List 'em all - Confused and slightly annoyed

Hello everyone. I’m fairly inexperienced when it comes to JavaScript and coding in general, but sometimes I also can’t feel but feeling that I am being lead astray by codeacademy. More often than not, it feels like much-needed information is withheld or simply skimmed over for the sake of keeping it simple, which mostly leads to - at times - very vague explanations and instructions. I feel the more I progress, the less I actually know.

1.Create a function list that takes a single parameter.

Wonderful. We created functions before. Personally, I feel we never went truly through the different ways we can define functions as we did with Objects, but in general, we can say that a function works like this:

var myFunction = function() {

};

But in the context of the exercise, I realized that I have no clue WHEN it is necessary to add parameters to a function.

2.In the body of the function, write a for/in loop.

Alright, and here I already found my first pet-peeve. I’m basically given a new concept of a loop and it wasn’t even remotely explained HOW it works? I went through some explanations, but I simply couldn’t understand them on a conceptual level.

3.In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out “bill” and “steve”.)

And this was the point, where I felt ENTIRELY lost, when I tried to get something done. Below is the code I created, feeling that NOTHING makes sense there.

/* My new Object. Alright, we used an object constructor here. Nothing outstanding yet. */
var friends = new Object();

/* Our objects have keys. They could be basically anything we want them to be. Numbers, Strings or just a bunch of booleans. */
friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "8324723978", 
    address: ["One Microsoft Way","Redmond","WA","98052"]
};

friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "8973265", 
    address: ["One Apple Way","Redmond","WA","98052"]
};

/* And there is the function 'list'. I actually wanted to use 'friends' as parameters, but I wanted to invoke the function and provide it the type of object myself. Technically, I could have used another type of object and Javascript encourages the re-usage of code. So I left my parameter as 'obj' */

/*My vague assumption is, that for.. in.. works like checking the length of an Array and incrementing the 'counter' we set for it in a regular for-loop. */

var list = function(obj){
    for(var prop in obj){
        console.log(prop);
        
    }    
}
list();

But I have no idea why the code works. Not even remotely. I invoked the function and yet the output were my two key items “bill” and “steve”. My question is… WHY? If invoked without a parameter, how does the program know what kind of object I am using? And if I invoke it with the function list with list(friends); it prints them out twice.

I also want to apologize if I seem ungrateful, I like this platform and I appreciate the creators of this page for this challenging piece of work, but sometimes I feel there could be some proper explanations to some introduced concepts.

5 Likes

You don’t invoke the output, and when it outputs twice, it is because Codecademy is checking that your function operates corrects. If you don’t give it input, it will still show results because of their code-checker.

3 Likes

So, when I actually invoke the function with the parameter

list(friends);

I only see it twice because of the code checker? That seems extremely misleading, but I thank you much for the very speedy help.

Yes, that is what is happening. Your welcome! :slightly_smiling:

If you don’t mind another question, let’s say I wanted to print out the first names of every key, how could I do that?

In your loop:

for (var person in list) {
    console.log(person.firstName)

Should work alright.

It just prints out “undefined”. Sheesh, I wonder if I can actually ever learn this properly. I seem to be too dense for this.

If you’re trying to only print out the first names of every person in your contacts, you should only for/in loop your key “firstName” from the friends. It turns out undefined because you don’t have a key called “person” your list so it doesn’t work when you log it.
It should look something like this

var list = function () {
for (var firstName in friends) {
console.log (firstName)
};
};

4 Likes

Thanks a lot! That worked.

Hey there, I tried this, but have discovered that my function is printing out my object name rather than the key firstName. Here is my code, can you help?:-

var friends = new Object();

friends.bill = new Object();
friends.steve = new Object();

friends.steve = {
firstName: “Steve”,
lastName: “Bob”,
number: “123”,
address: [‘17 Maungawhau Rd’, ‘Newmarket’, ‘Auckland’]
};

friends.bill = {
firstName: “Bill”,
lastName: “Bob”,
number: “234”,
address: [‘20 D Crockerton’, ‘Tooting Bec’]
};

var list = function () {
for (var firstName in friends) {
console.log(firstName);
}
};

list();

Of course!
so before, you wrote the bill and steve in constructor notation

and then you wrote bill and steve again but in object literal notation (basically with the “{}”) so you basically repeated the subobjects in the list friends twice. This is why it prints out the whole object instead of the key firstName.
Just delete this:

and keep the rest the same. It should work fine then :slightly_smiling:
btw, don’t worry if it prints it out twice because the object itself without the function would print the firstName

it still prints out “steve” and “bill” rather than “Steve” or “Bill”. And when I change it to console.log(lastName); it still prints out “steve” and “bill”.

Hi supasuma, I think I’ve worded my answer poorly. Codecademy wants you to print out the words “steve” and “bob” as their object names not their stored firstName key. But if you want to print out their names “Steve” and “Bob”, how I would code it would be completely different from what Codecademy wants:

function friend(firstName, lastName, number, address) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.number= number;
    this.address= address
}

var bill= new friend ("Bill", "Gates", "123456789873", [5, "ajfnksaifsnj"] )
var steve= new friend ("Steve", "Jobbs", "12345678765432", [5, "ajfnksaifsnj"] )

var contacts = [bill, steve];

function list() {
    for (var i = 0; i < contacts.length; i++) {
        console.log(contacts[i].firstName)
    }
}

I made keys to store in values with the “new” constructor notation so you can write codes for multiple people while saving a lot of energy. It should give you what you want :slightly_smiling: copy it in and try it out.

Problem I see with your example is, that you have been using concepts that people new to coding wouldn’t be able to know of, as they weren’t introduced yet up to this assignment. You have already been using classes and the concept of creating new instances of an object. While I appreciate your input, you shouldn’t post such solutions, when people are clearly not there yet.

dear, if you read the question from that section there, it asks you to simply print out “bob” and “steve” using the list function and not “Bob” and “Steve”. Your question itself is not part of this assignment in codecdemy. I simply answered your question on how to only print out the firstName key. My first answer is how you move on to the next section.

var list = function () {
for (var firstName in friends) {
console.log (firstName)
};
};

1 Like