Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
How can i make all three console.log into a single line?

// code

const arr1 = [17, 21, 34];
const printForecast = function (arr1) {
  for (var i = 0; i < arr1.length; i++) {
    console.log(`${arr1[i]} C on day ${arr1.indexOf(arr1[i]) + 1}`);
  }
};

printForecast(arr1);

// I want an output like this--> 17 C on day 1 21 C on day 2 34 C on day 3

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  1. Your function should ideally be returning a value.

  2. Use map to iterate over the array, create a new array of strings, and join that up into a new string before you return it.

const arr1 = [17, 21, 34];

function getForecast(arr1) {
  return arr1.map((num, i) => {
    return `${num} C on day ${i + 1}`;
  }).join(', ');
};

console.log(getForecast(arr1));

about 4 years ago · Juan Pablo Isaza Relatório

0

Construct the string inside the loop. Assign it to a variable and then print it outside the for loop.

const arr1 = [17, 21, 34];

const printForecast = function(arr1) {
  let string = "";
  for (var i = 0; i < arr1.length; i++) {
    string += `${arr1[i]} C on day ${arr1.indexOf(arr1[i]) + 1}, `;
  }
  console.log(string);
};

printForecast(arr1);

about 4 years ago · Juan Pablo Isaza Relatório

0

const arr1 = [17, 21, 34];

arr1.forEach((item, index) => {
  console.log(`${item} C on day ${index + 1}`);
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda