Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
44
Task/Palindrome-dates/JavaScript/palindrome-dates-1.js
Normal file
44
Task/Palindrome-dates/JavaScript/palindrome-dates-1.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
* Adds zeros for 1 digit days/months
|
||||
* @param date: string
|
||||
*/
|
||||
const addMissingZeros = date => (/^\d$/.test(date) ? `0${date}` : date);
|
||||
|
||||
/**
|
||||
* Formats a Date to a string. If readable is false,
|
||||
* string is only numbers (used for comparison), else
|
||||
* is a human readable date.
|
||||
* @param date: Date
|
||||
* @param readable: boolean
|
||||
*/
|
||||
const formatter = (date, readable) => {
|
||||
const year = date.getFullYear();
|
||||
const month = addMissingZeros(date.getMonth() + 1);
|
||||
const day = addMissingZeros(date.getDate());
|
||||
|
||||
return readable ? `${year}-${month}-${day}` : `${year}${month}${day}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns n (palindromesToShow) palindrome dates
|
||||
* since start (or 2020-02-02)
|
||||
* @param start: Date
|
||||
* @param palindromesToShow: number
|
||||
*/
|
||||
function getPalindromeDates(start, palindromesToShow = 15) {
|
||||
let date = start || new Date(2020, 3, 2);
|
||||
|
||||
for (
|
||||
let i = 0;
|
||||
i < palindromesToShow;
|
||||
date = new Date(date.setDate(date.getDate() + 1))
|
||||
) {
|
||||
const formattedDate = formatter(date);
|
||||
if (formattedDate === formattedDate.split("").reverse().join("")) {
|
||||
i++;
|
||||
console.log(formatter(date, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getPalindromeDates();
|
||||
Loading…
Add table
Add a link
Reference in a new issue