Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda