-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
42 lines (30 loc) · 1.03 KB
/
index.html
File metadata and controls
42 lines (30 loc) · 1.03 KB
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
<html>
<p>There have been <span id="sheep_count">0</span> sheep 🐑 and <span id="other_count">0</span> others.</p>
<input id="text" />
<button id="button">add animal</button>
<script>
var sheep_count, other_count;
function getNumberOrString(value) {
// Convert a string value to a number if possible
let number_value = Number(value);
if (Number.isNaN(number_value)) {
return value
} else {
return number_value
}
}
sheep_count = 0;
other_count = 0;
document.getElementById('button').addEventListener('click', (event) => {
if (getNumberOrString(document.getElementById('text').value) == 'sheep') {
sheep_count = (typeof sheep_count === 'number' ? sheep_count : 0) + 1;
let element_sheep_count = document.getElementById('sheep_count');
element_sheep_count.innerText = sheep_count;
} else {
other_count = (typeof other_count === 'number' ? other_count : 0) + 1;
let element_other_count = document.getElementById('other_count');
element_other_count.innerText = other_count;
}
});
</script>
</html>