Contact list. Whats wrong here, and where did my capital 'S' go?


#1


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

I hate to keep asking questions like this, however I seem to be wasting a lot of time on seemingly minor details, so here we are :confused:
lastName is undefined - why?
firstName displays as 'steve' even though the string is "Steve" - why?


var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "5",
        address: ["Road1", "Street1","Town1"]
    },
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "4",
        address: ["Road2", "Street2","Town2"]
    }
}

function list(friends) {
    for (var firstName in friends) 
        console.log (firstName)
}

var search = function(name) {
    for(var firstName in friends) {
        if(friends[firstName].firstName === name) {
            console.log ("First Name: " + [firstName])
            console.log ("Last Name: " + [lastName])
            console.log ("Number: " + [number])
            console.log ("Address: " + [address])
            
        }
    }   
};

search ("Steve")


#2

Here is my code it worked for me.

var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};

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

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

list(friends);
search("Steve");


#3

Hi, thanks for your response. Your code works, as does mine. However the console.log looks messy. Im trying to output like below as suggested on the 'victory' page.

First Name: Steve
Last Name: Jobs
Number: 4
Address: Road2, Street2, Town2


#4

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(408) 555-5555',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}

Mine says this


#5

My point exactly. I can get it to say that, but that's not what im trying to achieve.

"but we could have made the code look even nicer. How might you format the output to look like this?"

^^ This is what I am trying to achieve. I have already passed the course, I am just reluctant to move on without understanding why this isn't working as I expect. The list of names also loses its capital S & B, and i'd like to understand how/ why this is happening, as I thought it was simply reproducing my string, which is quite clearly "Bill" and "Steve"


#6

you have understand wrong!
change your friends property
from bill: to Bill
and
steve: to Steve:

Do you know firstName is just a variable of your first function named list not anything else?
so when you console firstName it doesn't mean you are consoling firstname of bill or steve.
you are just consoling friends property what is small Letter.
i think you have understnad now.
any question?
@dduk


#7

Ok that makes sense....i made the change and it worked (although that means im not outputting the firstName property, rather the name of the object, correct?).

Would you be able to help me understand how i output my contacts last name and/ or phone number then instead??

My search is built to check the firstName property, however if it finds a match i want it to output each property it holds.


#8

At first try yourself then i'll tell you.
remember D.R.Y method.
dnt use too much console.log
use for loop for last name,phone number etc.
@dduk


#9

Thought I was on to something, but clearly not. I remember D.R.Y, but am thinking one step at a time right now... once i get it to work, i can re-evaluate the way it is written to improve it (if that makes sense)

If I used a loop, wouldnt that just output the data, and not allow me to add "Last Name: " string etc? Id have to create a function to output it in this way right?


#10

if you want to do like this
you should take variable for all.
lastname ,phone number etc.
you have just take firstname variable not others.
@dduk


#11

Ive created a function to print the contact, and removed unnecessary console.logs

Im just missing how to actually get the contacts property from the object.

EDIT

is it because my friend (Steve) is an object within an object,, and I am trying to call the .lastName from within the first object?? that would make sense...


#12

Use it

var friends={};
friends.steve={
firstName:"steve",
lastName:"sarkar",
number:"9932969706",
address:["fulbari","malda","732154"]
};
friends.Bill ={
firstName:"bill",
lastName:"das",
number:"4532969736",
address:["bansbari","ucba","732155"]
};

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

@dduk


#13

That has just taken me back to square one. I can output the contact, I want to output it in a neat way as suggested on the victory screen.

Although, i now want to learn to output each individual property, as i do not know how to.

This is how your code is outputting...


#14

just understand my code you can do that what u want
@dduk


#15

If you can modify your code to output:

Last Name: sarkar

(and nothing else) for contact steve, im highly confident that i can figure the rest out from there. Im sorry if i am missing something, but i cannot see any method that allows me to do this.


#16

use it
it'll make sense :slight_smile:

if(friends[key1].firstName==="steve"){
var lastname=friends[key1].lastName;
console.log(lastname);
dnt be sorry you can ask me if you need. :slight_smile:
@dduk


#17

Thank you. Im still getting a reference error. Also, why does your output include the "" ??


#18

paste the update code here

call the printPerson function after the printPerson function not before
@dduk


#19

var friends = {
    Bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "5",
        address: ["Road1", "Street1","Town1"]
    },
    Steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "4",
        address: ["Road2, ", "Street2, ","Town2, "]
    }
}

function list(friends) {
    for (var key1 in friends) 
        console.log (key1)
}

var search = function(name) {
    for(var key1 in friends) {
        if(friends[key1].firstName === name) {
            var firstname=friends[key1].firstName;
            var lastname=friends[key1].lastName;
            var number=friends[key1].number;
            var address=friends[key1].address;
           printPerson()
        }
    }   
};

var printPerson = function (){
       console.log ("First Name: " + firstname + 
            "\nLast Name: " + lastname +
            "\nNumber: " + number +
            "\nAddress: " + address)
}
list(friends)
search ("Steve")

#20

var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: "5",
address: ["Road1", "Street1","Town1"]
},
Steve: {
firstName: "Steve",
lastName: "Jobs",
number: "4",
address: ["Road2, ", "Street2, ","Town2, "]
}
}

function list(friends) {
for (var key1 in friends)
console.log (key1)
}

var search = function(name) {
for(var key1 in friends) {
if(friends[key1].firstName === name) {
var firstname=friends[key1].firstName;
var lastname=friends[key1].lastName;
var number=friends[key1].number;
var address=friends[key1].address;
console.log ("First Name: " + firstname +
"\nLast Name: " + lastname +
"\nNumber: " + number +
"\nAddress: " + address)

    }
}

};

//var printPerson = function (){

//printPerson()
list(friends)
search ("Steve")

why are using printPerson function????
block the list function too.
you don't need it.
@dduk