Improve: /*

Tech assignment - Network speed
Provider: NAME_1
Author: NAME_2
Last update: 09/02/2023

*/

// defining types used
interface NetworkStation {
  x: number;
  y: number;
  r: number;
}

// locations of network stations + their reaches
const networkStations: NetworkStation[] = [
  // r = reach
  { x: 0, y: 0, r: 9 },
  { x: 20, y: 20, r: 6 },
  { x: 10, y: 0, r: 12 },
  { x: 5, y: 5, r: 13 },
  { x: 99, y: 25, r: 2 },
];

const getDistance = (input: NetworkStation, x: number, y: number) =>
  Math.sqrt(Math.pow(x - input.x, 2) + Math.pow(y - input.y, 2));

const calcSpeed = (input: NetworkStation, x: number, y: number) =>
  Math.pow(input.r - getDistance(input, x, y), 2);

// returns the most suitable (with highest non-zero speed) network station for a device at a given point (x, y).
export const findSuitableStation = (x: number, y: number) => {
  // define default values for the mostSuitableStation and highestSpeed variables
  let mostSuitableStation: NetworkStation | null = null,
    highestSpeed: number = 0;

  networkStations.forEach((station) => {
    const distance = getDistance(station, x, y);
    // checking if any stations are in reach
    if (distance <= station.r) {
      const speed = calcSpeed(station, x, y);
      // updates the most suitable station and rounds highest speed to nearest integer
      if (speed > highestSpeed) {
        mostSuitableStation = station;
        highestSpeed = Math.round(speed);
      }
    }
  });

  // return result
  return !mostSuitableStation
  