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

251
Visualizações
How do I get certain amounts of items from certain position in array?

(i’m very much just a beginner, infact I started a few days ago, so I’m sorry in advance if this seems messy. Im trying to do my best to explain)

So lets say i have a function that expects arguments like this:

getItems([items],getItemsFrom){
}

Also, lets say I have a fix amount of items i need to get and an array constructed from item1,item2…itemN, like

const ITEMS_TO_GET = 5;
const ITEMS_ARRAY = Array.from(items);

Now i need to implement some code that does this:

  1. Check if ITEMS_ARRAY have more than 5 elements.
  2. -> if not, then just add them all
  3. -> if yes, check if the getItemsFrom value is greater than 0
  4. If it is, count up 5 elements at a time as many times as getItemsFrom is (So if getItemsFrom is 2, count up to the 10th element, if its 3 count to the 15th)
  5. Once its done with the counting, sum up the next 5 elements and return the sum.
let itemSum = 0;

if(ITEM_ARRAY.length > 5){
// Some fancy code
} else if (ITEM_ARRAY > 0) {
  for(let i = 0; i < ITEM_ARRAY.length; i++){
    itemSum += ITEM_ARRAY[i];
  }
  return itemSum;
} else {return}

(All code you see here are not actual code from project. Im on my phone.)

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

0

if i have understand what you want, the biggest problem is that you don't want to have only 5 elements in you array you want to have at least getItemsFrom * 5 + 5. second, try to reorganize your test : you don't need to do all that if the function ask for 0 elements.

here some code for you

const ITEMS_TO_GET = 5;
var ITEMS_ARRAY = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
function getItems(array,getItemsFrom){
  if(getItemsFrom <= 0){
    console.log("getItemsFrom is inferior 0. abording");
    return;
  }

  if(array.length < getItemsFrom * ITEMS_TO_GET){
    console.log("no enough item in array");
    console.log("adding some to make up the minimum ("+(getItemsFrom * ITEMS_TO_GET + ITEMS_TO_GET)+")");
    var nbToAdd = (getItemsFrom * ITEMS_TO_GET + ITEMS_TO_GET)-array.length;
    for(var i = 0; i < nbToAdd;i++){
      array.push(i);
    }
  }
  var begin = getItemsFrom * ITEMS_TO_GET;
  var end = getItemsFrom * ITEMS_TO_GET+ITEMS_TO_GET;
  var sliced = array.slice(begin ,end);
  console.log("data from "+begin+" to "+end + " : " +sliced);
  console.log("sum : " + sliced.reduce((a, b) => a + b, 0));
}

getItems(ITEMS_ARRAY,4);

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