質問するログイン新規登録

回答編集履歴

2

全体を書き直した版のコードも追記

2019/10/05 07:02

投稿

tanishi_a
tanishi_a

スコア484

answer CHANGED
@@ -17,10 +17,50 @@
17
17
 
18
18
  ----
19
19
 
20
- ただ他の人も仰ってる通り、メンテがつらいコードですね・・。
20
+ あと本質と関係ないですが、
21
21
 
22
+ - 綴り間違い: `calender` → `calendar`
23
+ - 変数名 `holiday` は配列なので `holidays` のほうが統一感がある
24
+ - 変数名の `xxx_date` が、日付を指している場合と曜日を指している場合とが混在していて分かりづらい
22
25
 
23
26
  ----
24
27
 
28
+ ただ、他の人も仰っている通り、メンテがつらいコードですね・・。
29
+
30
+ 直すとしたらこうかなー、、と思って書いてみましたが、これはこれで・・。
31
+
32
+ ```JavaScript
33
+ function formatDate(d) {
34
+ return d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate();
35
+ }
25
- あと、本質と関係ないですが、 calender → calendar です。
36
+ function createCalendar() {
37
+ const calendarTable = document.getElementById('calendarTable');
38
+ const now = new Date();
39
+ const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
40
+ const beginningOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
41
+ const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
42
+
43
+ let holidays = [
44
+ '2019/10/14',
45
+ '2019/10/22',
46
+ ];
47
+
48
+ const datesTable = [];
49
+ for (let week = -1, date = beginningOfMonth; date <= endOfMonth; date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1)) {
50
+ const dayOfWeek = date.getDay();
51
+ if (datesTable.length === 0 || dayOfWeek === 0) {
52
+ datesTable.push(new Array(7).fill('<td></td>'));
53
+ week += 1;
54
+ }
55
+ const style = holidays.includes(formatDate(date)) ? 'background-color: pink;' : '';
56
+ datesTable[week][dayOfWeek] = `<td style="${style}">${date.getDate()}</td>`;
57
+ }
58
+ let html = '';
26
- エディタで検索して引っかからなくて、あれ?となりました。
59
+ for (const row of datesTable) {
60
+ const tr = calendarTable.insertRow(-1);
61
+ for (const cell of row) {
62
+ tr.innerHTML += cell;
63
+ }
64
+ }
65
+ }
66
+ ```

1

文字色ではなく背景色でしたね。訂正

2019/10/05 07:01

投稿

tanishi_a
tanishi_a

スコア484

answer CHANGED
@@ -10,7 +10,7 @@
10
10
  ```JavaScript
11
11
  date = date < 1 || date > last_date ? '' : date;
12
12
  if (holiday.includes(date)) {
13
- return '<td style="color: red;">' + date + '</td>';
13
+ return '<td style="background-color: pink;">' + date + '</td>';
14
14
  }
15
15
  return '<td>'+date+'</td>'
16
16
  ```