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 216 views
1 answer 243 views
1 answer 279 views
1 answer 249 views
1 answer 222 views
1 answer 215 views
...