-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
44 lines (33 loc) · 1.33 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const form = document.getElementById('formAtividade');
const nomes = []; /* cria uma string "[]" para registrar os nomes */
const numeros = []; /* cria uma string "[]" para registrar os numeros */
let linhas = '';
form.addEventListener('submit', function(e) {
e.preventDefault();
adicionaLinha();
atualizaTabela();
});
function adicionaLinha() {
const inputNome = document.getElementById('nomeContato');
const inputNumero = document.getElementById('numeroContato');
if (nomes.includes(inputNome.value)) { /* Verifica se o nome ja foi incluido anteriormente */
alert(`O contato ${inputNome.value} já existe!`);
}
else if (numeros.includes(inputNumero.value)) { /* Verifica se o numero ja foi incluido anteriormente */
alert(`O numero ${inputNumero.value} já existe!`);
}
else { /* Se não foi inserida, a função segue normalmente */
nomes.push(inputNome.value);
numeros.push(inputNumero.value);
let linha = '<tr>';
linha += `<td id="tNome">${inputNome.value}</td>`;
linha += `<td id="tNumero">${inputNumero.value}</td>`;
linhas += linha;
inputNome.value = '';
inputNumero.value = '';
}
}
function atualizaTabela() {
const corpoTabela = document.querySelector('tbody');
corpoTabela.innerHTML = linhas;
}