Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda