JohnMichael JohnMichael - 1 month ago 4
Javascript Question

Alerting data specifics inside array

I'm running into a javascript beginner's problem. I have this data stored in an array, and I'm trying to alert the data to see if it checks out.

I want to alert myself the average of the distance of all, not both planets even though both is all in this case, but I eventually want to have an array that keeps a lot more than just two. Right now, it just alerts the distance of the last record in the array list, which is weird. I thought it'd be an average for all.

Also, how do I program the least and highest numbers of the "Distance" property in the array, and then alert the "Host name" with it. So, if I have a button and I click on "Closest", the Host name of the lowest number in

"Distance [pc]"
will be alerted. I only need an example of the code for "Distance", so I'll know how to do the same for all other variables.

Thank you if you're willing to help out!

Btw, the list is JSON data. Maybe important to mention this.

// this array holds the json data, in this case stastics of exoplanets retrieved from nasa's website
var arr= [ {
"rowid": 684,
"Host name": "K2-15",
"Number of Planets in System": 1,
"Planet Mass or M*sin(i)[Jupiter mass]": null,
"Planet Radius [Jupiter radii]": 0.221,
"Planet Density [g": {
"cm**3]": null
"Distance [pc]": 437,
"Effective Temperature [K]": 5131,
"Date of Last Update": "7/16/2015"
"rowid": 687,
"Host name": "K2-17",
"Number of Planets in System": 1,
"Planet Mass or M*sin(i)[Jupiter mass]": null,
"Planet Radius [Jupiter radii]": 0.199,
"Planet Density [g": {
"cm**3]": null
"Distance [pc]": 134,
"Effective Temperature [K]": 4320,
"Date of Last Update": "7/16/2015"

//every record is put in a variable
var rowid;
var hostName;
var numberOfPlanetsInSystem;
var planetMass;
var planetRadius;
var distance;
var effectiveTemperature;

for(var i=0;i<arr.length;i++){

rowid= arr[i]["rowid"];
hostName= arr[i]["Host name"];
numberOfPlanetsInSystem= arr[i]["Number of Planets in System"];
planetMass= arr[i]["Planet Mass or M*sin(i)[Jupiter mass]"];
planetRadius= arr[i]["Planet Radius [Jupiter radii]"];
distance= arr[i]["Distance [pc]"];
effectiveTemperature= arr[i]["Effective Temperature [K]"];

//alert to test it out

let totalDistance = minDiatance = maxDistance = 0;
let closestHost = farthestHost = "";
for(let i = 0; i < arr.length; i ++) {
    totalDistance += arr[i]["Distance [pc]"]
    if (arr[i]["Distance [pc]"] < minDistance) {
        minDistance = arr[i]["Distance [pc]"];
        closestHost = arr[i]["Host name"];
    if (arr[i]["Distance [pc]"] > maxDistance) {
        maxDistance = arr[i]["Distance [pc]"];
        farthestHost = arr[i]["Host name"];
let meanDistance = totalDistance/arr.length;