My code works but im not sure why


#1


Contact List Number 7 Search For a Friend.

So the problem is that I was able to get my code to work but im not 100% sure why it works. so be more specific im not sure why the "argument" parameter in the "search" function has to be in the square brackets [ ] and not in parentheses (). Can someone offer up an explanation I dont want to go forward and end up lost because i dont undersand the basics back here.

        if(friends[argument].firstName === name)


#2

why would you use parentheses? parentheses are for function calls. Lets simplify the code a bit:

steve ={
   firstName:"Steve",
   lastName:"Jobs",
   number:"(978)534-8106",
   address:["3","Riverview Road","Sterling","MA","01564"]
}

now if we want to access a property of steve we have two choices:

steve.firstName
steve["firstName"]

if you want to read more about this you can do this here

i am going to borrow some explanation from this source:

You access the properties of an object with a simple dot-notation:

objectName.propertyName

which is what we did here:

steve.firstName

steve being the object, and firstName being the property.

Properties of JavaScript objects can also be accessed or set using a bracket notation:

objectName["propertyName"]

this are the two ways you can access properties of an object, not with parentheses.

if we use a loop, we have to use the bracket notation. Since the for loop uses a string.


#3

Thank you that makes more sense now.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.