Write a JavaScript function calendar ([day, month, year]) that returns a monthly calendar as HTML table (like in the examples below) by given day, month and year. Weeks start by “Sun” (Sunday) and end by “Sat” (Saturday).
The input comes as array of 3 numbers:
-
day (1 ≤ day ≤ 31)
-
month (1 ≤ month ≤ 12)
-
year (1900 ≤ year ≤ 2100)
The output should be an HTML table, holding the calendar rows and columns, like in the examples below. Display the weeks as table rows: <tr>…</tr>. Display the days as table cells: <td>…</td>. Display the days of the previous month with CSS class “prev-month”, the days of the next month with CSS class “next-month” and the current day with CSS class “today”. See the examples below.
Examples:
Input 1:
24
12
2012
Output 1:
<table>
<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
<tr><td class="prev-month">25</td><td class="prev-month">26</td><td class="prev-month">27</td><td class="prev-month">28</td><td class="prev-month">29</td><td class="prev-month">30</td><td>1</td></tr>
<tr><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td></tr>
<tr><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td></tr>
<tr><td>23</td><td class="today">24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td></tr>
<tr><td>30</td><td>31</td><td class="next-month">1</td><td class="next-month">2</td><td class="next-month">3</td><td class="next-month">4</td><td class="next-month">5</td></tr>
</table>
Preview 1:

Input 2:
4
9
2016
Output 2:
<table>
<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
<tr><td class="prev-month">28</td><td class="prev-month">29</td><td class="prev-month">30</td><td class="prev-month">31</td><td>1</td><td>2</td><td>3</td></tr>
<tr><td class="today">4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td></tr>
<tr><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td></tr>
<tr><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td></tr>
<tr><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td class="next-month">1</td></tr>
</table>
Preview 2:

HTML Skeleton:
To simplify your work, use the below HTML code and write the missing code in the calendar() function:
<!DOCTYPE html>
<html>
<head>
<title>Monthly Calendar</title>
<style>
.prev-month, .next-month { color: #CCC }
.today { font-weight: bold; background: #DDD; }
.title { background: #AAAAFF; margin: 10px 0; padding:5px }
table { border: 1px solid #CCC;}
td { text-align: center; }
#calendarCode { width: 100%; }
</style>
<script>
function calendar([day, month, year])
{
// TODO: return the HTML text holding the calendar table
}
</script>
</head>
<body>
Day: <input id="day" type="number" value="4" />
Month: <input id="month" type="number" value="9" />
Year: <input id="year" type="number" value="2016" />
<input type="button" value="Show"
onclick="let calendarHTML =
calendar([document.getElementById('day').value,
document.getElementById('month').value,
document.getElementById('year').value]);
document.getElementById('calendar').innerHTML = calendarHTML;
document.getElementById('calendarCode').innerText = calendarHTML" />
<div class="title">Calendar:</div>
<div id="calendar">Calendar will be shown here</div>
<div class="title">HTML:</div>
<textarea rows="12" id="calendarCode"></textarea>
</body>
</html>
Screenshot:
This is how your calendar should look in Web browser, when the calendar() function is implemented correctly:
