7. Search for a friends Syntax error + overall question on For/in


#1

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661

I am getting a syntax error. I have been staring at this problem for the whole day and cannot see what I am doing wrong. Can someone please help me. Admittedly, I have not been able to understand what the advantage of a for/in loop is or how it really works in comparison to a regular for loop. If someone wouldn't mind explaining that to me as well I would really appreciate it.


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 272-6788",
        address: ['one penthouse way', 'Seattle', 'WA', '98121']
    }
    steve: {
        fistName: "Steve",
        lastName: "Jobs",
        number: "(458) 568-1123",
        address: ['apple court', 'san francisco', 'CA', '98111']
    }
}

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

var search = function(name) {
    for(var key in friends) {
        if(friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key]
        }
    }
}
search("Bill");
search("Steve");.


#2

Three things stand out:
1.) I am not 100% sure, but you may need to put a comma between "bill" and "steve" (after bill's closing curly bracket)

2.) You don't have a semicolon after your return statement in your search function AND your console statement in your list function

3.) This may have been a mistake when you were posting the code but you have a random period at the end

Also, to my knowledge the for/in is mainly used for looping through lists/arrays.


#3

Thanks, I added the semicolons to the return and console.log statements.

Still getting "SyntaxError: Unexpected token" though as I am not sure what/ where you mean to out the comma for between the bill and steve. Can you clarify at all? thanks for the help.


#4

What I meant is to try this

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 272-6788",
        address: ['one penthouse way', 'Seattle', 'WA', '98121']
    },
    steve: {
        fistName: "Steve",
        lastName: "Jobs",
        number: "(458) 568-1123",
        address: ['apple court', 'san francisco', 'CA', '98111']
    }
}

See the comma after "bill: { }" is closed?


#5

Interestingly, this is what I am returning now. Is it supposed to be returning the curly brackets? Is it possible that the curly brackets are getting tied into the information that it is supposed to be returning, therefore messing up the result?

{ firstName: 'Bill',
lastName: 'Gates',
number: '(206) 272-6788',
address: [ 'one penthouse way', 'Seattle', 'WA', '98121' ] }
{ firstName: 'Bill',
lastName: 'Gates',
number: '(206) 272-6788',
address: [ 'one penthouse way', 'Seattle', 'WA', '98121' ] }


#6

Started form the beginning and reviewed a few things to get it right. Thanks for the help


#7

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