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

328
Visualizações
How to modularize code while creating HTML elements in JavaScript?

I am writing a piece of code to basically call in the top money earner and the top five money earners in a given data set. While writing the code, I realized that there were a couple of spots where I was rewriting the code, basically copying and pasting it. While that works, I wanted to throw the duplicate portion of the code and call it from a function. However, that is not working and I don't exactly know why. Here is the code that is duplicated:

for (let i = 0; i < len; i++) {
    html +=
      '<li class="top">' +
      '<h2>' +
      topSalaries[i][8] +
      '</h2>' +
      '<h3>' +
      topSalaries[i][11] +
      '</h3>';
  }

  container.innerHTML = '<ul id = "topSalaries">' + html + '</ul>';

Here is the function I made to be called. However, when I call it, it's not working as expected, where the information shows up on the webpage. I'm using VS Code and am running this on live server so when I save, the webpage automatically updates.

function createHtmlElements(len, html) {
  for (i = 0; i < len; i++) {
    html +=
      '<li class="top">' + 
      '<h2>' + 
      topFiveSalaries[i][8] + 
      '</h2>' + 
      '<h3>' + 
      topFiveSalaries[i][11] +
      '</h3>' +
      '</li>';
  }

  return html
}

function getTopSalaries(boston, container) {
  const people = boston.data;
  const len = 5; // only want top five
  let topFiveSalaries = sortPeople(people).slice(0,len);

  // create the list elements
  html = createHtmlElements(len, html);
  container.innerHTML = '<ul id = topSalaries">' + html + '</ul>';
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

For one thing topFiveSalaries is going to be undefined in the function createHtmlElements you've created, you must pass it to the function

about 4 years ago · Juan Pablo Isaza Relatório

0

Ok. So, Thanks Dave for the help. It looks like I also was missing a piece in that I needed to pass the array into the function as well. This is what I wrote and how I called it.

function getTopSalaries(boston, container) {
  const people = boston.data;
  const len = 5; // only want top five
  var topFiveSalaries = sortPeople(people).slice(0,len);
  let html = '';

  // create the list elements
  html = createHtmlElements(len, html, topFiveSalaries);
  container.innerHTML = '<ul id = topSalaries">' + html + '</ul>';
}

function getTopEarner(boston, container){
  const people = boston.data;
  const len = 1;
  let highEarner = sortPeople(people).slice(0,len);
  var html = '';

  // create the list elements
  createHtmlElements(len, html, highEarner);
  container.innerHTML = '<ul id = topSalaries">' + html + '</ul>';
}

// sort people by income in descending order
function sortPeople(people) {
    people.sort(function(a, b) {
      return b[11] - a[11];
    })

  return people
}

function createHtmlElements(len, html, array) {
  for (i = 0; i < len; i++) {
    html +=
      '<li class="top">' + 
      '<h2>' + 
      array[i][8] + 
      '</h2>' + 
      '<h3>' + 
      array[i][11] +
      '</h3>' +
      '</li>';
  }

  return html
}
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