この記事は
2020年3月13日が13日の金曜日だったので、javascriptで13日の金曜日を数えてみました。
自分の誕生日から今日までに何度13日の金曜日を過ごしたか確認できます。
コード
friday_13th.js
javascript:(function(){
let inputDate=prompt('誕生日は?', '2012/12/12'); //yyyy/m/d
let birthday = new Date(inputDate);
let today = new Date();
let jasonDate = [];
const listOf13th = (year)=>{
return Array(12).fill().map((_, i) => new Date(`${year}/${i+1}/13`));
}
for(let y=birthday.getFullYear(); y<=today.getFullYear(); y++){
let fridays = listOf13th(y).filter(
(d)=> (birthday < d && d < today && d.getDay() == 5)
);
jasonDate.push(...fridays);
}
alert('あなたは' + jasonDate.length + '回、13日の金曜日を生き抜きました。');
console.table(jasonDate.map( (v,i)=>{
return {date:v.toLocaleDateString()};
}));
})
()
まとめ
何事もなく過ごせました。よく頑張った。