-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
74 lines (56 loc) · 1.87 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const formulario = document.getElementById('formulario')
const footer =document.getElementById('footer')
const totalArray = []
const totalNumero = []
let linhas = '' //escopo global para ter seu valor resetado a cada evento
formulario.addEventListener('submit', function(e) {
e.preventDefault()
novaLinha()
atualizaLinha()
atualizaContatos()
})
function recarregarAPagina() {
window.location.reload()
}
function pressionaReset() {
let confirmacao = confirm('Tem certeza que deseja limpar a agenda?')
if (confirmacao == true){
recarregarAPagina()
}}
function novaLinha() {
const inputNome = document.getElementById('primeiro-nome')
const inputSobrenome = document.getElementById('segundo-nome')
const inputNumero = document.getElementById('numero-telefone')
let quantidade = 0
if (totalNumero.includes(inputNumero.value)) {
alert(`O número ${inputNumero.value} já está atribuído a um outro contato`)
} else {
totalNumero.push(inputNumero.value)
let linha = '<tr>'
linha += `<td>${inputNome.value}</td>`
linha += `<td>${inputSobrenome.value}</td>`
linha += `<td>${inputNumero.value}</td>`
linha += `</tr>`
quantidade += 1
totalArray.push(quantidade)
linhas += linha
}
inputNome.value = '';
inputSobrenome.value = '';
inputNumero.value = '';
}
function atualizaLinha() {
const Tabela = document.querySelector('tbody') //substituimos o tbody pela let linhas dentro do HTML
Tabela.innerHTML = linhas
}
function totalContatos() {
let somaContatos = 0
for (let i = 0; i < totalArray.length; i++) {
somaContatos += totalArray[i];
}
return somaContatos
}
function atualizaContatos() {
const contatos = totalContatos()
document.getElementById('total').innerHTML = 'Total de contatos: ' + contatos
}