Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

math.trunc() returning values with decimals

The Task

The first century spans from the year 1 up to and including the year 100, the second century – from the year 101 up to and including the year 200, etc.

Given a year, return the century it is in. However, for the else if block, its returning 19.64, rather than 20. What is the issue with this line?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const onCenturyCrossover = function (year) {
  return year % 2 === 0;
};

const century = function (year) {
  if (onCenturyCrossover(year) === true) {
    return year / 100;
  } else if (onCenturyCrossover(year) === false) {
    return Math.trunc(year / 100) + 1;
  }
};
console.log(century(2000));
console.log(century(1964));

>Solution :

const century = year => Math.floor(year/100) + 1;

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading