Adding spaces inside object variable


#1
"We did some basic logging of your contact list to the console, but we could have made it look even nicer. How might you format the output to look like this?

First Name: Steve
Last Name: Jobs
Number: (408) 555-5555
Address: 1 Infinite Loop
         Cupertino, CA 95014 "

How do I do this?
I know that I can change the whole string to say this, but I assume that that is not what they are going for.
Help would be very appreciated, thanks!


#2

Well let's start with what you've already attempted to do and where you currently sit with your code. This will narrow down our efforts to allow you to learn and pass the lesson.


#3

Yes!

var friends = {};
friends = {
    bill: {
    firstName:"Bill",
    lastName: "Gates",
    number: "Microsoft",
    occupation: "Programmer billionaire",
    address: ['One Microsoft Way', 'XBox One', 'Microsoft Games' ]
    }
};

I passed the course, but I want to make it so that it looks like in their example to the left in the final segment of the course, that it in this case would say:

First Name: Bill

Instead of:

firstName: 'Bill'

#4

you had a variable "list" there too right? supposedly below the friends object?

Well, The way they did it was slightly cheating. I'm sure they put their output into a string instead of printing it right from the object like you are.

The only way you can add a "space" to a variable is with an underscore "_".. It's not exactly the same thing but close enough if you wanted to make your program more readable.

The way it is done in the lesson is just a fast way for you to output all of the information from the object into the console with the list variable.

You could take a little bit longer and type it into a string like the following.

var string = "First Name: " + friends.bill.firstName;

and so on.


#5

Well, technically, you don't have to list it for each friend...

   for (var key in friends) {
                if (friends[key].firstName===name) {
                    console.log("First Name: " + friends[key].firstName);
                    console.log("Last Name: " + friends[key].lastName);
                    console.log("Phone: " + friends[key].number);
                    console.log("Address: " + friends[key].adress);
                    return friends[key];
                };

The above was part of my search function, inside an if/else statement to account for the possibility of not finding a match.


#6

Yes, I had a list(name) function that the segment was asking for.

I first thought there was perhaps another way, but I see now what they were asking for.

Thanks alot!


#7

Thank you very much!