Last active
April 30, 2018 22:42
-
-
Save alexko30/7ba41872ea70f49f8d9404b21b9d31db to your computer and use it in GitHub Desktop.
year Century
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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