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

139
Views
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 answers
Answer question

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