1+ <!DOCTYPE html>
2+ < html lang ="en-us ">
3+ < head >
4+ < meta charset ="UTF-8 " />
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6+ < style >
7+ table {
8+ border-collapse : collapse;
9+ }
10+
11+ td ,
12+ th {
13+ border : 1px solid black;
14+ padding : 3px ;
15+ text-align : center;
16+ }
17+
18+ th {
19+ font-weight : bold;
20+ background-color : # E6E6E6 ;
21+ }
22+ </ style >
23+ </ head >
24+ < body >
25+
26+ < div id ="calendar ">
27+
28+ </ div >
29+
30+ < script type ="text/javascript ">
31+
32+ /**
33+ * Escreva uma função createCalendar(elem, year, month).
34+ *
35+ * A chamada deve criar um calendário para um determinado ano/mês e colocá-lo dentro de elem
36+ *
37+ * O calendário deve ser uma tabela, onde uma semana é <tr> e um dia é <td>. O tampo da mesa deve ser <th> com nomes de dias da semana: o primeiro dia deve ser segunda-feira e assim por diante até domingo.
38+ *
39+ * Por exemplo, createCalendar(cal, 2012, 9) deve gerar no elemento cal o seguinte calendário:
40+ *
41+ * PS Para esta tarefa basta gerar o calendário, ainda não deve ser clicável.
42+ */
43+
44+ function drawCalendar ( qtdDaysMonth ) {
45+ let table = document . createElement ( 'table' ) ;
46+ let thead = document . createElement ( 'thead' ) ;
47+ let tbody = document . createElement ( 'tbody' ) ;
48+ table . prepend ( thead ) ;
49+ //NOME DOS DIAS DA SEMANA
50+ let nameDays = [ "MO" , "TU" , "WE" , "TH" , "FR" , "SA" , "SU" ] ;
51+ let trDays = document . createElement ( 'tr' ) ;
52+ for ( let i = 0 ; i < nameDays . length ; i ++ ) {
53+ let th = document . createElement ( 'th' ) ;
54+ th . textContent = nameDays [ i ] ;
55+ trDays . append ( th ) ;
56+ }
57+ thead . append ( trDays ) ;
58+ //QUANTIDADE DE SEMANAS NO MÊS
59+ let qtdSemanas = 0 ;
60+ if ( qtdDaysMonth > 28 ) {
61+ qtdSemanas = 5 ;
62+ } else {
63+ qtdSemanas = 4 ;
64+ }
65+ for ( let j = 0 ; j < qtdSemanas ; j ++ ) {
66+ let tr = document . createElement ( "tr" ) ;
67+ for ( let k = 0 ; k < 7 ; k ++ ) {
68+ let td = document . createElement ( "td" ) ;
69+ tr . append ( td ) ;
70+ }
71+ tbody . append ( tr ) ;
72+ }
73+ table . append ( tbody ) ;
74+ document . body . prepend ( table ) ;
75+ return table ;
76+ }
77+
78+ function anoEBissexto ( year ) {
79+ //etapa 1
80+ if ( ( year % 4 == 0 ) ) {
81+ //etapa 2
82+ if ( year % 100 == 0 ) {
83+ //etapa 3
84+ if ( year % 400 == 0 ) {
85+ //ano e bissexto
86+ return true ;
87+ } else {
88+ //ano não e bissexto
89+ return false
90+ }
91+ }
92+ else {
93+ //etapa 4
94+ //ano e bissexto
95+ return true ;
96+ }
97+ } else {
98+ //etapa 5
99+ //ano não e bissexto
100+ return false ;
101+ }
102+ }
103+
104+ function createCalendar ( elem , year , month ) {
105+ if ( ( year >= 1900 && year <= 2022 ) && ( month >= 1 && month <= 12 ) ) {
106+ //key: numberMonth -> value: qtdDaysMonth, firstDayMonth
107+ let daysMonths = new Map ( [
108+ [ 1 , [ 31 , 'SU' ] ] , [ 2 , [ 28 , 'WE' ] ] , [ 3 , [ 31 , 'WE' ] ] , [ 4 , [ 30 , 'SA' ] ] , [ 5 , [ 31 , 'MO' ] ] , [ 6 , [ 30 , 'TH' ] ] , [ 7 , [ 31 , 'SA' ] ] , [ 8 , [ 31 , 'TU' ] ] , [ 9 , [ 30 , 'FR' ] ] , [ 10 , [ 31 , 'WE' ] ] , [ 11 , [ 30 , 'WE' ] ] , [ 12 , [ 31 , 'FR' ] ]
109+ ] ) ;
110+ if ( anoEBissexto ) {
111+ daysMonths = new Map ( [
112+ [ 1 , [ 31 , 'SU' ] ] , [ 2 , [ 29 , 'SA' ] ] , [ 3 , [ 31 , 'WE' ] ] , [ 4 , [ 30 , 'SA' ] ] , [ 5 , [ 31 , 'MO' ] ] , [ 6 , [ 30 , 'TH' ] ] , [ 7 , [ 31 , 'SA' ] ] , [ 8 , [ 31 , 'TU' ] ] , [ 9 , [ 30 , 'FR' ] ] , [ 10 , [ 31 , 'WE' ] ] , [ 11 , [ 30 , 'WE' ] ] , [ 12 , [ 31 , 'FR' ] ]
113+ ] ) ;
114+ }
115+ let table = drawCalendar ( year ) ;
116+ let tds = table . querySelectorAll ( 'td' ) ;
117+ let ths = table . querySelectorAll ( "th" ) ;
118+ let cellStartMonth = 0 ;
119+ for ( let index = 0 ; index < ths . length ; index ++ ) {
120+ if ( ths [ index ] . textContent == daysMonths . get ( month ) [ 1 ] ) {
121+ //console.log(daysMonths.get(month)[1]);
122+ cellStartMonth = index ;
123+ //console.log(cellStartMonth);
124+ }
125+ }
126+ //obtendo quantidade de celulas para preenchimento
127+ let subCells = Array . from ( tds ) . slice ( cellStartMonth ) ;
128+ //quantidade dias no mês
129+ for ( let j = 0 ; j < daysMonths . get ( month ) [ 0 ] ; j ++ ) {
130+ subCells [ j ] . textContent = j + 1 ;
131+ }
132+ } else {
133+ alert ( 'Ano informado invalido!' ) ;
134+ }
135+ }
136+
137+ createCalendar ( calendar , 2010 , 3 ) ;
138+
139+ </ script >
140+
141+ </ body >
142+ </ html >
0 commit comments