Skip to content

Commit a3d3841

Browse files
committed
Exercicios vetores
1 parent 75fc043 commit a3d3841

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

cap06/exerc01.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-br">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="css/estilo.css">
8+
<title>Exercicio 01 Vetores</title>
9+
</head>
10+
11+
<body>
12+
<img src="img/serie-a-1.jpg">
13+
<h1>Jogos Eliminatórios</h1>
14+
<form>
15+
16+
<p>Clube:
17+
<input type="text" id="inClube">
18+
<input type="submit" value="Adicionar">
19+
</p>
20+
<input type="button" value="Listar Clubes" id="btListar">
21+
<input type="button" value="Montar tabela de jogos" id="btTabela">
22+
</form>
23+
<pre></pre>
24+
<script src="js/exerc01.js"></script>
25+
</body>
26+
27+
</html>

cap06/exerc02.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="pt-br">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="css/estilo.css">
8+
<title>Exercicios sobre vetores 02</title>
9+
</head>
10+
11+
<body>
12+
<img src="img/exerc.jpg" alt="atividade">
13+
<form>
14+
<p>Número:
15+
<input type="number" id="inNumero">
16+
<input type="submit" value="Adicionar">
17+
</p>
18+
<input type="button" value="Verificar ordem" id="btOrdem">
19+
</form>
20+
<pre></pre>
21+
<h3 id="OrdemNumbers"></h3>
22+
23+
<script src="js/exerc02.js"></script>
24+
</body>
25+
26+
</html>

cap06/img/serie-a-1.jpg

114 KB
Loading

cap06/js/exerc01.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Seleciona o formulário e o elemento <pre> (usado para exibir a lista ou os jogos)
2+
const frm = document.querySelector("form");
3+
const resp = document.querySelector("pre");
4+
5+
// Cria um array vazio chamado "tabela" para armazenar os nomes dos clubes
6+
const tabela = [];
7+
8+
// Adiciona um evento de submissão ao formulário
9+
frm.addEventListener("submit", (e) => {
10+
e.preventDefault(); // Impede o comportamento padrão do formulário (não recarrega a página)
11+
12+
// Obtém o valor do campo de entrada chamado "inClube" no formulário
13+
const clubes = frm.inClube.value;
14+
15+
// Adiciona o nome do clube ao array "tabela"
16+
tabela.push(clubes);
17+
18+
// Dispara um evento "click" no botão de listar (btListar) para atualizar a lista exibida
19+
frm.btListar.dispatchEvent(new Event("click"));
20+
21+
// Limpa o campo de entrada e foca nele
22+
frm.inClube.value = "";
23+
frm.inClube.focus();
24+
});
25+
26+
// Adiciona um evento de clique ao botão de listar
27+
frm.btListar.addEventListener("click", () => {
28+
// Verifica se a lista está vazia
29+
if (tabela.length == 0) {
30+
alert("Não há clubes na lista");
31+
inClube.focus(); // Foca no campo de entrada
32+
return;
33+
}
34+
35+
let lista = "";
36+
37+
// Cria uma string com os nomes dos clubes separados por quebra de linha
38+
for (const clubes of tabela) {
39+
lista += clubes + "\n";
40+
}
41+
42+
// Exibe a lista no elemento <pre>
43+
resp.innerText = lista;
44+
});
45+
46+
// Adiciona um evento de clique ao botão de criar jogos em pares
47+
frm.btTabela.addEventListener("click", () => {
48+
const tam = tabela.length;
49+
50+
// Verifica se há um número par de clubes na lista
51+
if (tam == 0 || tam % 2 == 1) {
52+
alert("Deve haver tabelas em pares");
53+
inClubes.focus(); // Foca no campo de entrada (presumivelmente o nome do clube)
54+
return;
55+
}
56+
57+
let jogos = "";
58+
59+
const ultimo = tam - 1;
60+
61+
// Cria pares de clubes para jogos, combinando o primeiro com o último, o segundo com o penúltimo, etc.
62+
for (i = 0; i < tam / 2; i++) {
63+
jogos += tabela[i] + " x " + tabela[ultimo - i] + "\n";
64+
}
65+
66+
// Exibe os jogos em pares no elemento <pre>
67+
resp.innerText = jogos;
68+
});

cap06/js/exerc02.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const frm = document.querySelector("form");
2+
const resp = document.querySelector("pre");
3+
const resp2 = document.querySelector("#outOrdem");
4+
5+
const numbers = [];
6+
7+
frm.addEventListener("submit", (e) => {
8+
e.preventDefault();
9+
10+
const numero = Number(frm.inNumero.value);
11+
12+
numbers.push(numero);
13+
14+
frm.btOrdem.dispatchEvent(new Event("click"));
15+
16+
frm.inNumero.value = "";
17+
frm.inNumero.focus();
18+
19+
let lista = " ";
20+
for (const number of numbers) {
21+
lista += number;
22+
}
23+
resp.innerText = "Numeros: " + lista;
24+
});
25+
26+
frm.btOrdem.addEventListener("click", () => {
27+
if (numbers.length == 0) {
28+
alert("Não há números na lista");
29+
inNumero.focus();
30+
return;
31+
}
32+
33+
let ordem = true;
34+
for (let i = 0; i < numbers.length - 1; i++) {
35+
if (numbers[i] > numbers[i + 1]) {
36+
ordem = false;
37+
break;
38+
}
39+
}
40+
41+
resp.innerText = ordem
42+
? "Ok! Números estão em ordem crescente"
43+
: "Atenção... Números não estão em ordem crescente";
44+
});

0 commit comments

Comments
 (0)