Is there any shortcut to indent a line in JavaScript?


#1


Hi everyone! My code works but I can't figure out this:
console.log(" " +address2);
How can I improve this line above?
Is there any predefined class or anything similar that I can use for indentation?
Any help would be great.
Thanks!

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Johnson",
    number: "(847) 456 1865",
    address: ["20 Main Loop"," Chicago, IL 90009"]
    };
friends.steve = {
    firstName: "Steve",
    lastName: "Hartl",
    number: "(512) 858 0154",
    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){
            var f = friends[prop].firstName;
            var l = friends[prop].lastName;
            var phone = friends[prop].number;
            var address1 = friends[prop].address[0];
            var address2 = friends[prop].address[1];
            
            console.log("First Name: "+f);
            console.log("Last Name: "+l);
            console.log("Number: "+phone);
            console.log("Address: "+address1);
            console.log("        "    +address2);
            
            return friends[prop];
            }
        }
    };

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


#2

Indenting as in what you do at the beginning of a paragraph?
For that, you would want to use tab.
Hope I helped :slight_smile:


#3

I was trying to get this output as it was stated in the example:

Address: 1 Infinite Loop
Cupertino, CA 95014

Could you take a look at my code and explain it shortly by showing my mistake? How does tab work? :slight_smile:


#4

The code would work fine for the output, provided you establish the first line.

And by tab, I meant the tab key.


#5

Forgive me if I'm being stupid but I still didn't get what you meant :slight_smile:

console.log("Address: "+address1);
console.log(" " +address2);

According to my first line, I've arranged my second line with quotation marks by leaving space in between
(" ")but then every time first line changes(gets shorter or longer), I have to arrange second line as well in order to be aligned with the first one. How do I prevent this?


#6

Mehmet, have you tried using the following snippet?

Whitespace (the extra spaces between things such as "a + b") normally has no effect on most languages. So "2 (pretend this is all spaces thanks) / (here too) 8" will be the same thing as "2 / 8."


#7

I truly appreciated your help!
I've actually tried what you suggested but it gives me this output:

Address: 1 Infinite Loop
Cupertino, CA 95014

What I need is this one:

Address: 1 Infinite Loop
Cupertino, CA 95014

I can get the second output with my current code above already but I was wondering what else can I do besides the way I used?

Edit: I just noticed that the outputs look exactly same on the post than what I am actually writing here so I guess I'll just stick to my own method for now. Thanks for all the help though :smiley:


#8

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