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

263
Visualizações
How to create an array from seed values that fills the array with given max bound?

I need a function to create an array using 2 values such that:

  • length: used to determine the new array length
  • maxIndexValue: used to determine the max value at a given array index before starting back at 0.

example:

// length and maxIndexValue are integer values.
function buildArray(length, maxIndexValue){
  // TODO: Implementation
  return array;
}

let newArray = buildArray(16, 3)

// newArray should be [0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3]
console.log(newArray);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This can be achieved using mod % operator.

function buildArray(length, maxIndexValue) {

  const arr = new Array(length);

  for (let a = 0; a < length; a++) {
    // Using the mod % operator
    // 0 % 4 = 0
    // 1 % 4 = 1
    // 2 % 4 = 2
    // 3 % 4 = 4
    // 4 % 4 = 0
    // 5 % 4 = 1
    // .. so on
    arr[a] = a % (maxIndexValue + 1);
  }

  return arr;
}

let newArray = buildArray(16, 3);
console.log(newArray);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can easily achieve the result using % operator as

Array(length)
    .fill(0)
    .map((_, i) => i % (maxIndexValue + 1));

or

new Array(length).fill(0).map((_, i) => i % (maxIndexValue + 1));

function buildArray(length, maxIndexValue) {
  return Array(length)
    .fill(0)
    .map((_, i) => i % (maxIndexValue + 1));
}

const length = 16,
  maxIndexValue = 3;
let newArray = buildArray(length, maxIndexValue);

console.log(newArray);

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