This is my react app - https://i.sstatic.net/9VpCd.jpg
I'm trying to return the highest and lowest latitude and the name of the city. I have it working to return the latitude but the name is coming up undefined... It is weird because in my jest testing, my function is working fine. I'm using React, Javascript and my API to load and store data... What I'm doing in this function is putting all the latitudes into an array then doing Math.max then looping through this.cities to see which latitude in the object matches the highest latitude that we put into mostNorthLat, then getting the name of the city that matches the lat.
This is my javascript file with my pure functions:
//Loads cities in API and injects it into this.cities
async loadCities() {
const data = await postData(url + "all");
//Create a dictionary of cities and keep track of the last key
const cities = {};
data.forEach(x => {
cities[x.key] = x;
this.lastKey = (x.key > this.lastKey) ? x.key : this.lastKey;
});
this.cities = cities;
}
//This is my add function I use for adding new cities to my API, which then React uses to create my cards for each object in my cities object in the API
async addOrUpdate(city) {
let theUrl;
if (city.key) {
theUrl = url + 'update'
} else {
theUrl = url + 'add'
this.lastKey++;
city.key = this.lastKey;
city.population = Number(city.population)
}
await postData(theUrl, city);
this.cities[city.key] = city;
}
//My functions that are running the most North and most South
getMostNorthern() {
let latitudeArray = []
let theName;
for (let key in this.cities) {
latitudeArray.push(this.cities[key].latitude)
}
let mostNorthLat = Math.max.apply(null, latitudeArray);
for (let key in this.cities) {
if (mostNorthLat === this.cities[key].latitude) {
theName = this.cities[key].name
}
}
if (this.length() > 0) {
return `${theName} at ${mostNorthLat}°`
}
}
getMostSouthern() {
let latitudeArray = []
let theName;
for (let key in this.cities) {
latitudeArray.push(this.cities[key].latitude)
}
let mostSouthLat = Math.min.apply(null, latitudeArray);
for (let key in this.cities) {
if (mostSouthLat === this.cities[key].latitude) {
theName = this.cities[key].name
}
}
if (this.length() > 0) {
return `${theName} at ${mostSouthLat}°`
}
}
Here is what a city object looks like:
{
"key": 1,
"latitude": "1",
"longitude": "1",
"name": "City",
"population": 49500
},
Each city object is held in a main object called 'cities'
constructor() {
this.cities = {};
this.lastKey = 0;
}
So this is an what 3 cities inside the object looks like:
{
'1': City {
name: "Gangster's Paradise",
latitude: -15,
longitude: 5,
population: 10,
key: 1
},
'2': City {
name: 'Pho City',
latitude: 5,
longitude: 52,
population: 1050,
key: 2
},
'3': City {
name: 'Frisbee Land',
latitude: 0,
longitude: 12,
population: 1000000,
key: 3
}
}
My React portion:
function CitiesApp() {
const classes = useStyles();
const [message, setMessage] = useState('')
const [count, setCount] = useState(0)
const [total, setTotal] = useState()
const [northest, setNorthest] = useState()
const [southest, setSouthest] = useState()
useEffect(() => {
//Load cities from the API - re-render when count is updated
async function fetchData() {
try {
await cityCtrl.loadCities()
updateSummary()
} catch (e) {
userMsg('Turn on the server')
}
}
fetchData();
}, [count]);
async function onSave(city) {
await cityCtrl.addOrUpdate(city)
setCount(count + 1)
updateSummary()
}
async function deleteCard(thekey) {
await cityCtrl.deleteCard(thekey)
setCount(count + 1)
updateSummary()
}
async function moveIn(thekey, num) {
await cityCtrl.movedIn(thekey, num)
setCount(count + 1)
updateSummary()
}
async function moveOut(thekey, num) {
await cityCtrl.movedOut(thekey, num)
setCount(count + 1)
updateSummary()
}
function updateSummary() {
setTotal(cityCtrl.getTotalPopulation())
setNorthest(cityCtrl.getMostNorthern())
setSouthest(cityCtrl.getMostSouthern())
}
}
this.citieslook like?