-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
111 lines (98 loc) · 2.41 KB
/
script.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var now = new Date();
var datetime = now.toDateString();
document.getElementById("date").innerHTML = datetime;
var connect = " - ";
var pounds = "lbs";
var numbers = [];
var change = [];
let net = 0;
const inputBox = document.getElementById("weight");
const listContainer = document.getElementById("log-container");
function findChange()
{
for(var i = 0; i < numbers.length - 1; i++)
{
change[i] = numbers[i + 1] - numbers[i];
}
}
function calculateAverage(array)
{
const sum = array.reduce((acc, curr) => acc + curr, 0);
const average = sum / array.length;
return average;
}
function restart()
{
var numbers = [];
var change = [];
}
function roundTo(n, digits) {
if (digits === undefined) {
digits = 0;
}
var multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
return Math.round(n) / multiplicator;
}
function calculate()
{
if(numbers.length < 2)
{
document.getElementById("avgNet").innerHTML = "N/A";
}
else
{
findChange();
const ans = roundTo(calculateAverage(change), 1);
document.getElementById("avgNet").innerHTML = ans;
net = ans;
if(ans > 0)
{
document.getElementById("avgNet").style.color = "#007502";
}
else
{
document.getElementById("avgNet").style.color = "#b00000";
}
}
}
function addWeight()
{
if(inputBox.value === '')
{
alert("Enter weight.");
}
else
{
let li = document.createElement("li");
li.innerHTML = datetime + connect + inputBox.value + pounds;
listContainer.appendChild(li);
numbers.push(inputBox.value)
calculate();
}
inputBox.value = "";
saveData();
}
listContainer.addEventListener("click", function(e)
{
if(e.target.tagName == "LI")
{
e.target.remove();
saveData();
}
}, false);
function saveData()
{
localStorage.setItem("data", listContainer.innerHTML);
localStorage.setItem("avgNet", net);
localStorage.setItem("numbers", numbers);
localStorage.setItem("change", change);
}
function displaySaved()
{
listContainer.innerHTML = localStorage.getItem("data");
document.getElementById("avgNet").innerHTML = localStorage.getItem("avgNet");
numbers = localStorage.getItem("numbers");
change = localStorage.getItem("change");
}
displaySaved();