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

131
Views
Javascript Algorithm - If the subtraction of indices is negative, return as elements of array as possible

Imagine a list of items:

const data = [0, 1, 2, 3, 4, 5]

One variable represents the main item:

const initialIndex = 1;

And the other variable, the number of extra elements that will also be collected behind of the "main item".

const extraCount = 2;

So, if for example, I call the method "algorithm" as follows:

function algorithm(data, initialIndex, extraCount) { ... }


console.log(algorithm(data, 2, 1));

It will return me [1, 2] // The initial index and the first element behind it

Or, if for example I call again with

console.log(algorithm(data, 4, 3));

It will return me [1, 2, 3, 4] // The initial element and 3 items behind it

How can I do, in a simple way, to fix this use case

 console.log(algorithm(data, 1, 2)); // Negative index...

But... I need to return at least the possible items.

I mean:

For console.log(algorithm(data, 1, 2)); I get [0, 1] // Initial index, and the first item before (not two because the second one has a negative index)

For console.log(algorithm(data, 2, 5)); I get [0, 1, 2]

For console.log(algorithm(data, 0, 10)); I get [0]

For console.log(algorithm(data, 3, 7)); I get [0, 1, 2, 3]

EDIT

This is what I have tried

const data = [0, 1, 2, 3, 4];
const initialIndex = 2;
const extraCount = 2;
    
const start = initialIndex - extraCount >= 0 ? initialIndex - extraCount : initialIndex;

const end = initialIndex + 1;

console.log(data.slice(start, end));

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have it correct. Just replace initialIndex by 0 in else condition

const data = [0, 1, 2, 3, 4];

let func = (initialIndex,extraCount) => {
    
const start = initialIndex - extraCount >= 0 ? initialIndex - extraCount : 0;

const end = initialIndex + 1;

console.log(data.slice(start, end));

};

func(2,2);
func(1,2);
func(3,7);

Note: There are a lot of other ways to do it, but this is the one you were going for so I just pointed out the issue

about 4 years ago · Juan Pablo Isaza Report

0

You can use slice(-count) to return elements from the end of array, something like this:

if (extraCount > initialIndex) {
  return [
    // This will return elemnts from beginning of array up to initialIndex
    ...data.slice(0, initialIndex),

    // This will return the missing elements looped from the other side of array
    ...data.slice(extraCount - initialIndex)
  ];
}

You should also incorporate other sanity checks into your code, or example, total number of elements should not exceed array length, etc.

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!