How to get Clean output. 8/8 Victory!


#1

Using a console.log approach, I can very manually print out the array and its items easily.
NOTE: My array and keys are based on cars in my garage, not a contact list.

var search = function (name) {
    for (var car in garage) {    
        if (name === garage[car].model) {
            console.log("model: " + garage[car].model);
            console.log("year: " + garage[car].year);
            console.log("HP: " + garage[car].HP);
            console.log("engine: " + garage[car].engine);
            console.log("____________________________");
        }
        else {
        }
    }
};

Instead of using console.log over and over again, I would like to use a for loop. I've tried this several times, but haven't been able to get it to print the same way (the closest was getting it to print just the values on their own line).
Has anyone used a loop to successfully print their array?


#2

Here is all of my code to help you understand my variables and arrays.

var garage = {
  miata: {
      model: "Mx-5",
      year: "1997",
      HP: "147",
      engine: "1.8L twin turbo"
  },
  mazda6: {
      model: "Mazda 6",
      year: "2009",
      HP: "170",
      engine: "2.5L i4"
  },
  s2k: {
      model: "S2000",
      year: "2003",
      HP: "221",
      engine: "AP1"
  }
};

var list = function (garage) {
    for (var car in garage) {
        console.log(car);
    }
};

var search = function (name) {
    for (var car in garage) {    
        if (name === garage[car].model) {
            console.log("model: " + garage[car].model);
            console.log("year: " + garage[car].year);
            console.log("HP: " + garage[car].HP);
            console.log("engine: " + garage[car].engine);
            console.log("____________________________");
        }
        else {
        }
    }
};



search("S2000");
search("Mx-5");
search("Mazda 6")

The output looks like this:

model: S2000
year: 2003
HP: 221
engine: AP1
____________________________
model: Mx-5
year: 1997
HP: 147
engine: 1.8L twin turbo
____________________________
model: Mazda 6
year: 2009
HP: 170
engine: 2.5L i4
____________________________

#3

You could do it like so:

console.log("model: " + garage[car].model + "/n"
           + console.log("year: " + garage[car].year + "/n" 
           + "HP: " + garage[car].HP + "/n"
           + "engine: " + garage[car].engine); + "/n"
           + "____________________________")

#4

Thanks for the reply. That would work, but would be essentially the same thing as replacing console.log with "\n".
I'm looking for a loop that would clean up the code and prevent us from manually writing everything out.


#5

So you want the output to look like that every time. Honestly, there's not much you can do to make it look like that other than how you have it.

You could do a few things, but at this point I can't think of how you would do what you're proposing to make everything faster. I can't think of how you could add anything that's not in the object to the end of the object without using the console.log statement in a function. I do apologize for the wasted time.


#6

Thanks for your feedback lolman. In Python, there is a way to easily print your key and it's value, ie "model: S2000". I was hoping JS would have something similar.


#7

Oh good lord I thought you meant adding the line underneath, hold on I think I've got a way to do that.

try:

            console.log(car + garage[car].model);
            console.log(car + garage[car].year);
            console.log(car + garage[car].HP);
            console.log(car + garage[car].engine);
            console.log("____________________________");

#8

It seems like you still don't understand my question. I'm looking for a for/while loop that will print out all of the key: value pairs in an array.
What we're currently doing is repeatedly using log, or console.log to write out "blank" + "blank" etc etc. It seems that this isn't a learned skill in the JS course on codecademy.

Also, in the code you provided, "car" is not defined. You would have to replace that with "model: ", "year: ", etc. (ie writing everything out manually)


#9

I see.. I'm not sure if this will work (I'll need to test it), but try this code, I hope it works for you.

for (var car in garage){
    for (var info in car){
        console.log(info + ": " + car);
    }
}

See if that fixes your issue.