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

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

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 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