1
0

More than 3 years have passed since last update.

ブックマークレットで13日の金曜日を数える

Posted at

この記事は

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()};
  }));
})
()

まとめ

何事もなく過ごせました。よく頑張った。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0