Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!