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

190
Views
¿Cómo detectar el cambio de signo en la matriz de javascript?

Tengo un ejemplo de matriz de javascript como se muestra a continuación.

 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...];

Me gustaría crear una función para detectar el cambio de signo y regresar con la posición, por ejemplo, ['index','num1','num2'] Por ejemplo.

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

el regreso debe ser

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

Cualquier consejo u orientación sobre esto sería muy apreciado, gracias.

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

0

Tal vez esto te ayude:

 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!