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

94
Views
por qué JavaScript se comporta de manera diferente

por qué me muestra 36 aunque el número mínimo es 27

 var combination = [27, 36] for (let x in combination) { if (combination[x] < 50) { var min = Math.min(combination[x]) } } console.log(min)

Intenté esto de varias maneras como

 var combination = [27, 30, 40, 44, 3, 239, 329, 2, 5, 20923, 96] for (let x in combination) { if (combination[x] < 50) { var min = Math.min(combination[x]) } } console.log(min) //output-- 5 //it should be 2

en este tercer ejemplo agrego (-) a 2

 var combination = [27, 30, 40, 44, 3, 239, 329, -2, 5, 20923, 96] for (let x in combination) { if (combination[x] < 50) { var min = Math.min(combination[x]) } } console.log(min) // output-- still 5 // it should be -2

nuevamente, cuando estoy agregando (-) a otros números como en -96 o -5, la salida estaba bien (-96), pero cuando estoy agregando (-) a 2, no me muestra -2 en la salida, sino que me muestra 5

no solo en javascript probé esto con lua, php pero el resultado fue el mismo que js

alguien me puede explicar por que pasa esto y como solucionarlo

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

0

No está comparando valores para determinar el mínimo, sino simplemente reemplazando la variable min con el último número en la matriz que es menor que 50 . Esto se puede arreglar de la siguiente manera:

 let min = undefined; for (let x in combination) { if (combination[x] < 50) { min = min == undefined ? combination[x] : Math.min(min, combination[x]) } }

Usando filter y reduce , esto se puede hacer mucho más corto:

 combination.filter(x => x < 50).reduce((x, y) => Math.min(x, y))
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!