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

147
Vistas
Why does not my function math.abs return true?

The function timeout decides if the absolute difference of the given dates (date1 and date2), is larger than the given absolute difference so it needs to return true if the difference between date1 and date2 is greater than the parameter timeLimit

I coded the following:

but in my test files, (1) and (2) still fails but the other passes and I dont know why. If I change the

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

to

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

then (1) and (2) passes but the other fail like below. how can I code it that everything is passed?

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

0

You need to Math.abs the limiet too

Also you had a logical error in your (2) - you did not have more than one minute's difference and you have < not <= so we need a difference

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