I’d like to understand this bit of a code:
var serach = function(name){
for (var firstName in friends){
if (friends.firstName === name){
return friends(key);
search('Steve');
Specifically
for (var firstName in friends){
if (friends.firstName === name){
return friends(key);
My questions are:
What exactly is for (var firstName in friends) doing?
What is friends.firstName referring to? Why the period?
What is friends(key) doing? Why is formated like that?
Keep in mind I have everything functioning (some how) I’d just like to understand these small parts.
There is no way that this code can work.
its looping over the properties of friends
object. You could insert a print statement to see:
for (var firstName in friends){
console.log(firstName);
}
this will log the properties of friends
object, bill
and steve
the object.propertyName
has been taught to access a property of an object?
the for in
loop will assign property of friends
object to loop iterator (firstName
), then we can use the loop iterator to access the value belonging to properties of friends object, we can do this manually using the full stop notation:
console.log(friends.bill);
console.log(friends.steve);
if your friends list will grow, this isn’t very handy, which is why we use a loop
however, the for in loop will assign string values to the loop iterator, so the full stop (.
) notation will not use, you have to use square brackets.
friends(key)
would be a function call with argument key
, which is just wrong. Friends isn’t a function, its an object.
var serach
, you have a typo in the function name
lots of closing curly brackets (}
) are missing.
I didn’t want to post my entire code. But here is what the entire code is doing:
var friends = new Object();
friends.bill = {
firstName: 'Bill',
lastName: 'Will',
number: '666',
address: ['fake', 'place', '00000']
};
friends.steve = {
firstName: 'Steve',
lastName: 'Even',
number: '999',
address: ['real', 'area', '11111']
};
var list = function(name){
for (var bill in friends){
console.log(bill);
}
};
list(friends);
var serach = function(name){
for (var firstName in friends){
if (friends.firstName === name){
return friends(key);
}
}
};
search('Steve');
This still shouldn’t work, there are still too many mistakes with the search
function, which already starts with a typo in the name of the function itself.
stetim94:
the for in
loop will assign property of friends
object to loop iterator ( firstName
), then we can use the loop iterator to access the value belonging to properties of friends object, we can do this manually
Odd. Here is the screen shot of it working?
![19%20AM|633x500]
Interesting enough when I changed the typo. It doesn’t let me continue.
It’s not working, something else is doing what you see
It may be (haven’t visited exercises lately) that codecademy keeps feeding code into the same environment, and that you had previously defined search
In my first answer i covered problems of your code, so far you have responded to none of them. The result you see is from the list
function.
@ionatan is right, because if i run your code:
http://jsbin.com/hicozodige/edit?js,console
i get an error. The course you take is no longer supported or maintained, as such its not very wise to rely only on the exercise validation. The truth is that your search function is not working correctly.
Sorry. I read over them and have a better understanding now.
I was confused as to why it was allowing me to continue with several errors.
Thanks.
Do you have further questions about the code?
That should be all. Thanks
system
closed
June 18, 2018, 5:45pm
#12
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.