File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
7+ < title > JavaScript</ title >
8+ </ head >
9+ < body >
10+
11+ < div class ="container ">
12+
13+ < form id ="form-01 ">
14+ < input type ="text " placeholder ="digite um número " name ="n1 ">
15+ < input type ="text " placeholder ="digite um número " name ="n2 ">
16+ < input type ="text " placeholder ="digite um número " name ="n3 ">
17+ < input type ="text " placeholder ="digite um número " name ="n4 ">
18+ < input type ="submit " value ="Enviar ">
19+
20+
21+ < div id ="resultado "> </ div >
22+
23+
24+
25+ </ form >
26+
27+
28+
29+ </ div >
30+
31+
32+
33+
34+
35+
36+ < script type ="text/javascript " src ="../js/script.js "> </ script >
37+
38+ </ body >
39+ </ html >
Original file line number Diff line number Diff line change 1+ document . addEventListener ( 'submit' , function ( evento ) {
2+
3+ evento . preventDefault ( ) ; //Formulário para de ter o comportamento padrão do html
4+ evento . stopPropagation ( ) ; //Faz com que todo submit de form caia nessa função
5+
6+ let form = document . getElementById ( 'form-01' ) ;
7+
8+ let dados = new FormData ( form )
9+
10+ let objeto = { } ; //Assim como array, string, número também é um tipo de variável
11+
12+ let notas = [ ] ;
13+
14+ for ( let key of dados . keys ( ) ) {
15+ objeto [ key ] = dados . get ( key ) ;
16+
17+ //Adicionar itens o array
18+ notas . push ( parseInt ( dados . get ( key ) ) ) ;
19+ }
20+
21+ console . log ( notas ) ;
22+ console . log ( objeto ) ;
23+
24+ function calcularMedia ( notas ) {
25+
26+ var soma = 0
27+ for ( c = 0 ; c < notas . length ; c ++ ) {
28+ soma += notas [ c ]
29+ }
30+
31+ media = soma / notas . length //Utilizar notas.length para que ele calcule dinamicamente a quantidade que ela recebeu
32+
33+ return media ;
34+ }
35+
36+ function aprovacao ( notas ) {
37+ let media = calcularMedia ( notas ) ; // escopo da função
38+ let condicao = media >= 7 ? "aprovado" : "reprovado" ;
39+ return 'Média: ' + media + ' - Resultado: ' + condicao ;
40+ }
41+
42+ document . getElementById ( 'resultado' ) . innerHTML = aprovacao ( notas ) ;
43+ aprovacao ( notas ) ;
44+
45+ } ) ;
You can’t perform that action at this time.
0 commit comments