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

145
Views
¿Por qué mi función math.abs no devuelve verdadero?

El tiempo de espera de la función decide si la diferencia absoluta de las fechas dadas (fecha1 y fecha2) es mayor que la diferencia absoluta dada, por lo que debe devolver verdadero si la diferencia entre fecha1 y fecha2 es mayor que el parámetro timeLimit

Codifiqué lo siguiente:

pero en mis archivos de prueba, (1) y (2) todavía fallan pero el otro pasa y no sé por qué. Si cambio el

 return Math.abs(date1 - date2) > limiet;

a

 return Math.abs(date1 - date2) < limiet;

luego (1) y (2) pasan pero el otro falla como se muestra a continuación. ¿Cómo puedo codificarlo para que todo se pase?

 function timeout(date1, date2, timeLimit) { const limiet = timeLimit * 60000; return Math.abs(date1 - date2) > limiet; } console.log(timeout(new Date("2019-09-01T08:30:00"), new Date("2019-09-01T11:30:00"), 60), "should return false") console.log(timeout(new Date("2019-09-01T11:30:00"), new Date("2019-09-01T08:30:00"), 60), "should return false") console.log(timeout(new Date("2019-09-01T11:30:00"), new Date("2019-09-01T08:30:00"), 190), "should return true") console.log(1,timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), -5), "should return true") console.log(2,timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), -1), "should return true") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 0), "should return false") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 1), "should return false") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 2), "should return false") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 3), "should return true")

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Necesitas Math.abs the limite también

También tuvo un error lógico en su (2): no tuvo más de un minuto de diferencia y tiene < no <= por lo que necesitamos una diferencia

 const ms2MMSS = ms => { let minutes = Math.floor(ms / 60000); let seconds = ((ms % 60000) / 1000).toFixed(0); return minutes + ":" + (seconds < 10 ? '0' : '') + seconds; } function timeout(date1, date2, timeLimit) { const limiet = Math.abs(timeLimit * 60000); console.log(ms2MMSS(limiet),ms2MMSS(Math.abs(date1.getTime()-date2.getTime()))); return Math.abs(date1 - date2) < Math.abs(limiet); } console.log(timeout(new Date("2019-09-01T08:30:00"), new Date("2019-09-01T11:30:00"), 60), "should return false") console.log(timeout(new Date("2019-09-01T11:30:00"), new Date("2019-09-01T08:30:00"), 60), "should return false") console.log(timeout(new Date("2019-09-01T11:30:00"), new Date("2019-09-01T08:30:00"), 190), "should return true") console.log(1,timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), -5), "should return true") console.log(2,timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:00:00"), -2), "should return true") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 0), "should return false") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 1), "should return false") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 2), "should return false") console.log(timeout(new Date("2019-08-31T23:59:00"), new Date("2019-09-01T00:01:00"), 3), "should return true")

about 4 years ago · Santiago Trujillo 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!