1

I need some help with this snippet, I have written so far this code:

const parqueAutomotor = [];

parqueAutomotor[0] = {Marca: "Peugeot",
                      Modelo: "206",
                      Puertas: 4,
                      Precio: "$200.000,00"},

parqueAutomotor[1] =    {Marca: "Honda",
                        Modelo: "Titan",
                        Cilindrada: "125c",
                        Precio: "$60.000,00"},

parqueAutomotor[2] =   {Marca: "Peugeot", 
                        Modelo: "208", 
                        Puertas: 5, 
                        Precio: "$250.000,00"},

parqueAutomotor[3] =   {Marca: "Yamaha",
                        Modelo: "YBR",
                        Cilindrada: "160c",
                        Precio: "$80.500,50"
                        };


var i, item;

for (i = 0; i < parqueAutomotor.length; i++) {
    for (item in parqueAutomotor[i]) {
    console.log(item + ": " + parqueAutomotor[i][item] + " // ");
    }
}

I was asked to have in the console this output:

Marca: Peugeot // Modelo: 206 // Puertas: 4 // Precio: $200.000,00
Marca: Honda // Modelo: Titan // Cilindrada: 125c // Precio: $60.000,00
Marca: Peugeot // Modelo: 208 // Puertas: 5 // Precio: $250.000,00
Marca: Yamaha // Modelo: YBR // Cilindrada: 160c // Precio: $80.500,50

Instead I get:

Marca: Peugeot // 
Modelo: 206 // 
Puertas: 4 // 
Precio: $200.000,00 // 
Marca: Honda // 
Modelo: Titan // 
Cilindrada: 125c // 
Precio: $60.000,00 // 
Marca: Peugeot // 
Modelo: 208 // 
Puertas: 5 // 
Precio: $250.000,00 // 
Marca: Yamaha // 
Modelo: YBR // 
Cilindrada: 160c // 
Precio: $80.500,50 // 

How do you suggest to fix it?

I need to respect the format I was given, Thanks!

1
  • each console.log is a new line Commented Aug 4, 2020 at 23:26

3 Answers 3

1

Store every line in a variable. The double slashes " //"should be only added before every entry as seperator from the car if it's not the beginnig of the line, so I can prevent the seperator at beginnig and at the end of each line. After building a line together just print it on the console.

const parqueAutomotor = [];

parqueAutomotor[0] = {Marca: "Peugeot",
                      Modelo: "206",
                      Puertas: 4,
                      Precio: "$200.000,00"},

parqueAutomotor[1] =    {Marca: "Honda",
                        Modelo: "Titan",
                        Cilindrada: "125c",
                        Precio: "$60.000,00"},

parqueAutomotor[2] =   {Marca: "Peugeot", 
                        Modelo: "208", 
                        Puertas: 5, 
                        Precio: "$250.000,00"},

parqueAutomotor[3] =   {Marca: "Yamaha",
                        Modelo: "YBR",
                        Cilindrada: "160c",
                        Precio: "$80.500,50"
                        };


var i, item;

for (i = 0; i < parqueAutomotor.length; i++) {
    let row ="";
    let startRow = true;
    for (item in parqueAutomotor[i]) {
        if (!startRow)
            row += " // ";
        else
            startRow = false;
        row += item + ": " + parqueAutomotor[i][item];
    }
    console.log(row);
}

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.forEach to iterate over your array, using Object.entries to get a list of key/value pairs and mapping each of them to a string, then outputting the final string using Array.join:

const parqueAutomotor = [];

parqueAutomotor[0] = {
    Marca: "Peugeot",
    Modelo: "206",
    Puertas: 4,
    Precio: "$200.000,00"
  },

  parqueAutomotor[1] = {
    Marca: "Honda",
    Modelo: "Titan",
    Cilindrada: "125c",
    Precio: "$60.000,00"
  },

  parqueAutomotor[2] = {
    Marca: "Peugeot",
    Modelo: "208",
    Puertas: 5,
    Precio: "$250.000,00"
  },

  parqueAutomotor[3] = {
    Marca: "Yamaha",
    Modelo: "YBR",
    Cilindrada: "160c",
    Precio: "$80.500,50"
  };


parqueAutomotor.forEach(o => console.log(Object.entries(o).map(a => `${a[0]}: ${a[1]}`).join(' // ')));

Comments

0

Since each console.log() prints a line, you have to build the string first and print the entire line at once.

const parqueAutomotor = [];

parqueAutomotor[0] = {
    Marca: "Peugeot",
    Modelo: "206",
    Puertas: 4,
    Precio: "$200.000,00"
  },

  parqueAutomotor[1] = {
    Marca: "Honda",
    Modelo: "Titan",
    Cilindrada: "125c",
    Precio: "$60.000,00"
  },

  parqueAutomotor[2] = {
    Marca: "Peugeot",
    Modelo: "208",
    Puertas: 5,
    Precio: "$250.000,00"
  },

  parqueAutomotor[3] = {
    Marca: "Yamaha",
    Modelo: "YBR",
    Cilindrada: "160c",
    Precio: "$80.500,50"
  };


var i, item;

for (i = 0; i < parqueAutomotor.length; i++) {
  let row = "";
  for (item in parqueAutomotor[i]) {
    row += item + ": " + parqueAutomotor[i][item] + " // ";
  }
  console.log(row)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.