Skip to content

Instantly share code, notes, and snippets.

@alexko30
Last active April 30, 2018 22:42
Show Gist options
  • Save alexko30/7ba41872ea70f49f8d9404b21b9d31db to your computer and use it in GitHub Desktop.
Save alexko30/7ba41872ea70f49f8d9404b21b9d31db to your computer and use it in GitHub Desktop.
year Century
function yearCentury(year) {
if (year >= 1 && year <= 100) {
console.log(1, 'century');
} else {
const yearArray = String(year).split('');
const arrayPoint = yearArray.length - 2;
let checkValue = 0;
for (var i = arrayPoint; i < yearArray.length; i++) {
if (yearArray[i] == 0) {
checkValue += 1;
}
}
if (checkValue == 2) {
let filteredArray = [];
for (let i = 0; i < arrayPoint; i++) {
filteredArray.push(yearArray[i]);
}
let century = 0;
century = filteredArray.toString().replace(/,/g, '');
console.log(century, 'century');
} else {
let filteredArray = [];
for (let i = 0; i < arrayPoint; i++) {
filteredArray.push(yearArray[i]);
}
let century = 0;
century = filteredArray.toString().replace(/,/g, '');
century = parseInt(century) + 1;
console.log(century, 'century');
}
}
}
yearCentury(22045); // 221 century
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment