-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathratingsCalcs.js
105 lines (86 loc) · 2.6 KB
/
ratingsCalcs.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
import React, { useEffect, useState } from "react";
import Fire from "../Firebase";
export const ratingsCalc = ({ route }) => {
const { city } = route.params;
const [ratingsCalc, setRatings] = useState([]);
const [averagesCalc, setAverages] = useState({});
const fetchRatings = async () => {
const ratings = await Fire.getRatings(city.name);
return ratings;
};
useEffect(() => {
fetchRatings().then((ratings) => setRatings(ratings));
}, []);
const fetchAverages = async () => {
const ratings = await Fire.getRatings(city.name);
const roverArr = [];
const costArr = [];
const funArr = [];
const internetArr = [];
const safetyArr = [];
const weatherArr = [];
for (let i = 0; i < ratings.length; i++) {
const singleRating = ratings[i];
// const rover = singleRating.rating;
const cost = singleRating.rating.cost;
const fun = singleRating.rating.fun;
const internet = singleRating.rating.internet;
const safety = singleRating.rating.safety;
const weather = singleRating.rating.weather;
costArr.push(cost);
funArr.push(fun);
internetArr.push(internet);
safetyArr.push(safety);
weatherArr.push(weather);
let total = 0;
for (let i = 0; i < costArr.length; i++) {
total += costArr[i];
}
let averageCost = total / costArr.length;
total = 0;
for (let i = 0; i < funArr.length; i++) {
total += funArr[i];
}
let averageFun = total / funArr.length;
total = 0;
for (let i = 0; i < internetArr.length; i++) {
total += internetArr[i];
}
let averageInternet = total / internetArr.length;
total = 0;
for (let i = 0; i < safetyArr.length; i++) {
total += safetyArr[i];
}
let averageSafety = total / safetyArr.length;
total = 0;
for (let i = 0; i < weatherArr.length; i++) {
total += weatherArr[i];
}
let averageWeather = total / weatherArr.length;
total = 0;
roverArr.push(
averageCost,
averageFun,
averageInternet,
averageSafety,
averageWeather
);
for (let i = 0; i < roverArr.length; i++) {
total += roverArr[i];
}
let averageAll = parseFloat(total / roverArr.length).toFixed(2);
setAverages({
rover: averageAll,
cost: averageCost,
fun: averageFun,
internet: averageInternet,
safety: averageSafety,
weather: averageWeather,
});
}
};
useEffect(() => {
fetchAverages();
}, []);
};
export default ratingsCalc;