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

202
Views
How to detect sign toggle in javascript array?

I have example of javascript array as below.

var exampleArr1 = [5,4,1,-2,-5,-7,5,1,2,-3,2,4...];
var exampleArr2 = [-5,-4,1,2,5,7,-5,1,2,3,-2,4...];

I would to create function to detect sign change and return with position, e.g. ['index','num1','num2'] For example.

function getSignChange(exampleArr1){
 ...
 return someArray;
}

return shoud be

[
  [2,1,-2],  //index = 2 change from 1 to -2
  [5,-7,5],  //index = 5 change from -7 to 5
  [8,2,-3],  //index = 8 change from 2 to -3
]

Any advice or guidance on this would be greatly appreciated, Thanks.

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

0

Maybe this will help you:

var exampleArr1 = [5,4,1,-2,-5,-7,5,1,2,-3,2,4];
var exampleArr2 = [-5,-4,1,2,5,7,-5,1,2,3,-2,4];

function getSignChange(arr){
 let positive = arr[0] >= 0; 
 return arr.map((item, index) => {
  if (positive && item < 0 || !positive && item >= 0) {
    positive = arr[index] >= 0
    return [index-1, arr[index-1], item]
  }
  
 }).filter(x => x != null);
}

console.log(getSignChange(exampleArr1));
console.log(getSignChange(exampleArr2));

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!