-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
73 lines (56 loc) · 1.83 KB
/
functions.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
function hideFirstForm (){
document.getElementById('number-of-members').style.display = 'none';
}
function showSecondForm () {
document.getElementById('reveal-after').style.display = 'inline';
}
function createARowForEachMember () {
const numberOfFamilyMembers = Number(document.getElementById('family-members').value);
const $forRows = document.getElementById('add-rows');
for (let i = 0; i < numberOfFamilyMembers; i++) {
let index = i + 1;
const label = document.createElement('label');
label.textContent = 'Member ' + index;
$forRows.appendChild(label);
const li = document.createElement('li');
const input = document.createElement('input');
input.type = 'number';
input.className = 'age';
li.appendChild(input);
$forRows.appendChild(li);
}
}
function HTMLCollectionIntoArray(HTMLCollection) {
let array = [];
for (let i = 0; i < (HTMLCollection.length); i++) {
let item = Number(HTMLCollection.item(i).value)
array.push(item);
}
return array;
}
function getOldestMember(array) {
const oldest = Math.max.apply(Math, array);
return oldest;
}
function getYoungestMember(array) {
const youngest = Math.min.apply(Math, array);
return youngest;
}
function getAgeAverage(array) {
let arraySum = 0;
for (let i = 0; i < array.length; i++) {
let arrayItem = Number(array[i]);
arraySum += arrayItem;
}
average = (arraySum / array.length).toFixed(0);
return average;
}
function showResults(oldest, youngest, average) {
document.getElementById('oldest-member').value = oldest;
document.getElementById('youngest-member').value = youngest;
document.getElementById('age-average').value = average;
}
function clearDataForNewCalculation (array) {
array = [];
}
// VALIDATION FUNCTIONS: