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

113
Views
Para obtener una matriz vacía

Escribí un código pero encontré algo extraño.

 function countPositivesSumNegatives(input) { let count = 0; let positive = 0; let negative = 0; for (let i = 0; i < input.length; i++) { if (input[i] < 0) { negative += input[i]; } if (input[i] > 0) { count++; positive = count; } if (input == 0) { return []; } } return [positive, negative]; } console.log(countPositivesSumNegatives([0, 0]));

¿Por qué la salida es [0, 0] en lugar de []? Estoy tratando de obtener una matriz vacía []

La salida tiene que ser:

contarPositivosSumaNegativos([1, 2, 3, -1, -3]) -> [3, -4]

contarPositivosSumaNegativos([0, 0]) -> []

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

0

(ACTUALIZADO)

Dado que la función debe devolver [] solo si todos los elementos son == 0 , puede verificar al final del ciclo si positive y negative son == 0 , lo que significa que no se han contado números positivos ni se han sumado números negativos:

 function countPositivesSumNegatives(input) { let count = 0; let positive = 0; let negative = 0; for (let i = 0; i < input.length; i++) { if (input[i] < 0) { negative += input[i]; } if (input[i] > 0) { count++; positive = count; } } if (positive === 0 && negative === 0) { return []; } else { return [positive, negative]; } } console.log(countPositivesSumNegatives([0, 0]));
about 4 years ago · Juan Pablo Isaza Report

0

La única vez que devuelve [] es cuando la condición (input[i] == 0) coincide con cualquier elemento.

Sin embargo, dado el nombre de la función, solo desea omitir dichos elementos.

Si y solo si el argumento no contiene otros elementos excepto 0 , devuelve la matriz vacía.

Así que el siguiente código modificado funcionará:

 function countPositivesSumNegatives(input) { let count = 0; let positive = 0; let negative = 0; for (let i = 0; i < input.length; i++) { if (input[i] < 0) { negative += input[i]; } if (input[i] > 0) { count++; } } positive = count; // Hoisted out of the loop. Unless your function contains more code, you do not need the 'count' variable at all. let seen_nonzero = ((positive !== 0) || (negative !== 0)); // Flags whether a non-0 element is present in your input. let a_r = seen_nonzero ? [positive, negative] : [] ; return a_r } console.log(countPositivesSumNegatives([0, 0])); console.log(countPositivesSumNegatives([1, 2, 3, -1, -3])); console.log(countPositivesSumNegatives([-1, -2, 0, 1, 2]));

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!