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.