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

149
Visualizações
JavaScript trying to find the highest sales made in a particular branch

I am trying to create a class to find out the branch made the most in revenue.I believe, I need to sum all branch across multiple days and identify which branch had the highest sales overall. But I am kinda lost. Would anyone be able to help me? Thanks for the help

class SalesItem {
    constructor(branch, totalSales, date) {
        this.branch = branch;
        this.totalSales = totalSales;
        this.date = date
    }
}

function CalculateBestBranch(sales) {
var branchSales = { key: "", value: 0 };



// TODO: order branchSales by value, highest first
// TODO: return the key of the highest value
const highestValue = 0;
return highestValue;

}

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

0

class SalesItem {
  constructor(branch, totalSales, date) {
    this.branch = branch;
    this.totalSales = totalSales;
    this.date = date;
  }
}

const sales = [
  new SalesItem('branch-1', 60, '2022-01-26'),
  new SalesItem('branch-2', 90, '2022-01-26'),
  new SalesItem('branch-1', 20, '2022-01-27'),
  new SalesItem('branch-2', 30, '2022-01-27'),
];

function calculateBestBranch(sales) {
  const branchSales = [];

  sales.forEach((sale) => {
    if (!branchSales.find((e) => e.key === sale.branch)) {
      branchSales.push({ key: sale.branch, value: sale.totalSales });
    } else {
      let i = branchSales.findIndex((e) => e.key == sale.branch);
      branchSales[i].value += sale.totalSales;
    }
  });

  const sortedBranchSales = branchSales.sort((a, b) => b.value - a.value);

  return sortedBranchSales[0];
}

calculateBestBranch(sales); // { key: 'branch-2', value: 120 }
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