-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (92 loc) · 2.92 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
112
113
114
115
116
117
"use strict";
// Prevent refreshing of page after submitting
const form = document.getElementById("myForm");
function handleForm(event) {
event.preventDefault();
}
form.addEventListener("submit", handleForm);
// Declaration of variables
let nameCat;
let age;
let weight;
let isSterilised;
let activity;
let isOverWeight;
let nameCatFood;
let caloriesOfFood;
// Function that calculates the factor of fisiological status
const calcFactorOfFisiologStatus = function () {
if (
age === "adult" &&
isSterilised === "intact" &&
activity === "active" &&
isOverWeight === "no"
) {
return 1.4;
} else if (
age === "senior" &&
isSterilised === "intact" &&
activity === "active" &&
isOverWeight === "no"
) {
return 1.2;
} else if (
(age === "adult" || age === "senior") &&
isSterilised === "spayed" &&
activity === "active" &&
isOverWeight === "no"
) {
return 1.2;
} else if (
(age === "adult" || age === "senior") &&
(activity === "sedentary" || isOverWeight === "yes")
) {
return 1;
} else if (age === "youngerKitten") {
return 2.5;
} else if (age === "olderKitten") {
return 2;
}
};
// CLICK SUBMIT BUTTON
document.querySelector(".submit").addEventListener("click", function () {
// Values of variables from the form
nameCat = document.querySelector(".nameCat").value
? document.querySelector(".nameCat").value
: "Your cat";
age = document.querySelector(".age").value;
weight = Number(document.getElementById("weight").value);
isSterilised = document.querySelector(".isSterilised").value;
activity = document.querySelector(".lifestyle").value;
isOverWeight = document.querySelector(".overweight").value;
nameCatFood = document.querySelector(".nameFood").value
? document.querySelector(".nameFood").value
: "food";
caloriesOfFood = Number(document.querySelector(".calories").value);
// Resting energy reuirments declaration
const rer = weight * 30 + 70;
// Fisiolgical factor's function calling
const f = calcFactorOfFisiologStatus();
console.log(f);
// Function that calculates grams of food
const calcGramsOfFood = function () {
return Math.round(((f * rer) / caloriesOfFood) * 1000);
};
//Grams' of food function calling
const gramsOfFood = calcGramsOfFood();
// Form that hiddes
document.querySelector("#hideForm").style.display = "none";
// Result content
document.querySelector("#output").textContent =
weight > 0 && caloriesOfFood > 0
? `${nameCat} should eat ${gramsOfFood} grams of ${nameCatFood} daily 😺`
: `Upss!😅 Please try again to enter correctly the weight and calories of food`;
// Refresh button that appers after submitting
document.querySelector("#refreshButton").style.display = "block";
});
// CLICK ON TRY AGAIN BUTTON
document
.querySelector("#refreshButton")
.addEventListener("click", function refreshPage() {
window.location.reload();
});