// https://www.codewars.com/kata/586a1af1c66d18ad81000134/javascript
function driver(data) {
let licence = '';
const [name, midlename, surname, db, sex] = data;
if (surname.length >= 5) {
licence += surname.slice(0, 5).toUpperCase();
} else {
licence += surname.toUpperCase() + '9'.repeat(5 - surname.length);
}
const dateBr = new Date(db);
const year = dateBr.getFullYear().toString();
const month = dateBr.getMonth() + 1;
const monthStr = (dateBr.getMonth() + 1).toString().padStart(2, '0');
licence += year[year.length - 2];
if (sex === 'M') {
licence += monthStr;
} else {
licence += (month + 50).toString();
}
const date = dateBr
.getDate()
.toString()
.padStart(2, '0');
licence += date;
licence += year[year.length - 1];
licence += name[0];
licence += midlename[0] || '9';
licence += '9';
licence += 'AA';
return licence;
}
console.log(driver(['John', 'James', 'Smith', '01-Jan-2000', 'M']));
console.log(driver(['Johanna', '', 'Gibbs', '13-Dec-1981', 'F']));
// SMITH001010JJ9AA
// // SMITH01010JJ9AA
// 1 2 3 4 5 6 7 8 9 10 11 12 13
// G I B B S 8 6 2 1 3 1 J 9 9AA
// G I B B S 8 6 2 1 2 1 J 9 9AA
Click Run to execute JavaScript.
Code runs in an isolated environment.
Code runs in an isolated environment.