-
Notifications
You must be signed in to change notification settings - Fork 0
/
contador.html
32 lines (29 loc) · 1.3 KB
/
contador.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" id="string"><br>
<button onclick="CountLetters(document.querySelector('#string').value)" style="margin-top: 10px">Contar</button><br><br>
<p style="font-size: 9pt">Verifique o console para conferir a contagem (não inclua acentuação no texto)</p>
<script>
function CountLetters(s){ //CAN'T USE ACENTUATION OR SPECIAL CHARACTERS
var letters = {'A': 0,'B': 0,'C': 0,'D': 0,'E': 0,'F': 0,'G': 0,'H': 0,'I': 0,'J': 0,'K': 0,'L': 0,'M': 0,'N': 0,'O': 0,'P': 0,'Q': 0,'R': 0,'S': 0,'T': 0,'U': 0,'V': 0,'W': 0,'X': 0,'Y': 0,'Z': 0}
var string = s.toUpperCase().trim();
for(l of string){ //Each letter of the given string
for(letter in letters){
letters[letter] += l === letter ? 1 : 0;
}
}
for(letter in letters){ //REMOVE UNECESSARY LETTER COUNTERS
if(letters[letter] == 0) delete letters[letter];
}
console.log(letters); //SHOW HOW MANY LETTERS IT HAS IN THE STRING
}
</script>
</body>
</html>