Discuss Scratch

StarscreamClone
Scratcher
1000+ posts

JavaScript Question

Alright, so through some sniffing around the Codecademy QA forums, I finally managed to finish this lesson. However, I do not quite understand how the function at the end works, and I would very much like to. Here's the code, and if you can explain it, I would be very grateful. Thank you!

var friends = {
bill: {
firstName: ‘Bill’,
lastName: ‘Cipher’,
number: ‘555-555’,
address:
},

steve: {
firstName: ‘Steve’,
lastName: ‘Creeper’,
number: ‘555-555’,
address:
},

soundwave: {
firstName: ‘Soundwave’,
lastName: '',
number: ‘555-555’,
address:
},
};

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

NOTE: There is code in the address column and Soundwave's lastName variable, but they are arrays so they won't show up.

Last edited by StarscreamClone (Dec. 29, 2013 19:22:42)


Opportunistic Starscream is opportunistic.
DCPU-16
Scratcher
100+ posts

JavaScript Question

The function at the end iterates over all of the keys of the object passed to it as an argument and prints them out, eg:

list(friends);

Would print out:

bill
steve
soundwave

The variable “key” is set to the current key being iterated over. This is then printed out.

(assuming you put values in for address before running it, otherwise it errors out)

Last edited by DCPU-16 (Dec. 29, 2013 20:37:32)

StarscreamClone
Scratcher
1000+ posts

JavaScript Question

DCPU-16 wrote:

The function at the end iterates over all of the keys of the object passed to it as an argument and prints them out, eg:

list(friends);

Would print out:

bill
steve
soundwave

The variable “key” is set to the current key being iterated over. This is then printed out.

(assuming you put values in for address before running it, otherwise it errors out)
Ah, thank you, that helps a lot.
Yes, there was. They were arrays, so they didn't show because they were in brackets.

Opportunistic Starscream is opportunistic.
turkey3_test
Scratcher
1000+ posts

JavaScript Question

I was following that exact tutorial by Leng Lee, and to be honest with you, when it got to this part I felt like he didn't explain everything as well and rushed it. I am still paused at about 60%

StarscreamClone
Scratcher
1000+ posts

JavaScript Question

turkey3_test wrote:

I was following that exact tutorial by Leng Lee, and to be honest with you, when it got to this part I felt like he didn't explain everything as well and rushed it. I am still paused at about 60%
Yeah, I don't know HOW I passed. If you need some help, I'm up to Objects Part 1, so I should be able to assist you.

Opportunistic Starscream is opportunistic.
turkey3_test
Scratcher
1000+ posts

JavaScript Question

StarscreamClone wrote:

turkey3_test wrote:

I was following that exact tutorial by Leng Lee, and to be honest with you, when it got to this part I felt like he didn't explain everything as well and rushed it. I am still paused at about 60%
Yeah, I don't know HOW I passed. If you need some help, I'm up to Objects Part 1, so I should be able to assist you.
Yeah, I was looking all over the Q/A on objects. I am still pretty confused on them.

Powered by DjangoBB

Standard | Mobile