How to get century from year in Node.js

1 Answer

0 votes
function getCenturyFromYear(year) {
  	return Math.floor((year - 1) / 100) + 1;
}

const arr = [100, 101, 340, 570, 1000, 1005, 1999, 2000, 2001, 2022];

arr.forEach(function(yr) {
  	console.log('year ' + yr + ' is in ' + getCenturyFromYear(yr) + ' century');
});


  
  
  
  
/*
run:
  
year 100 is in 1 century
year 101 is in 2 century
year 340 is in 4 century
year 570 is in 6 century
year 1000 is in 10 century
year 1005 is in 11 century
year 1999 is in 20 century
year 2000 is in 20 century
year 2001 is in 21 century
year 2022 is in 21 century
  
*/

 



answered May 5, 2022 by avibootz

Related questions

1 answer 128 views
1 answer 154 views
1 answer 169 views
1 answer 160 views
1 answer 150 views
1 answer 130 views
...