Skip to content

I made my homework fix all debugging #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 59 additions & 36 deletions Debugging2/homework/train-stations-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,31 @@
*/

const trainStations = {
"Amsterdam":
"Amsterdam":
{// [latitude, longitude]
coordinates: [52.3791283, 4.8980833],
connections: ["Rotterdam", "Utrecht"]},
"Rotterdam":
{coordinates: [51.92235, 4.4661983],
connections: ["Amsterdam", "Utrecht"]},
"Utrecht":
{coordinates: [52.0894444, 5.1077981],
connections: ["Amsterdam", "Rotterdam", "Arnhem", "Oberhausen"]},
"Arnhem":
{coordinates: [51.984034, 5.8983483],
connections: ["Utrecht", "Oberhausen"]},
coordinates: [52.3791283, 4.8980833],
connections: ["Rotterdam", "Utrecht"]
},
"Rotterdam":
{
coordinates: [51.92235, 4.4661983],
connections: ["Amsterdam", "Utrecht"]
},
"Utrecht":
{
coordinates: [52.0894444, 5.1077981],
connections: ["Amsterdam", "Rotterdam", "Arnhem", "Oberhausen"]
},
"Arnhem":
{
coordinates: [51.984034, 5.8983483],
connections: ["Utrecht", "Oberhausen"]
},
"Oberhausen":
{coordinates: [51.4983534, 6.8131958],
connections: ["Arnhem", "Utrecht"]}
{
coordinates: [51.4983534, 6.8131958],
connections: ["Arnhem", "Utrecht"]
}
};

function latitude(coordinates) {
Expand All @@ -47,31 +56,33 @@ function distanceInMeters(coord1, coord2) {
function radians(degrees) {
return (Math.PI / 180) * degrees;
}

const deltaLatitude = radians(latitude(coord2) - latitude(coord1));
const deltaLongitude = radians(longitude(coord2) - longitude(coord1));
const a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) +
Math.cos(radians(latitude(coord1))) *
Math.cos(radians(latitude(coord2))) *
Math.sin(deltaLongitude / 2) *
Math.sin(deltaLongitude / 2) *
Math.sin(deltaLongitude / 2);
const c = Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) * 2;

return earthRadiusInMeters * c;
}

// const distanceBetweenAmsterdamAndUtrecht = distanceInMeters([52.3791283, 4.8980833], [52.0894444, 5.1077981]);
// console.log('distance Between Amsterdam And Utrecht: ',metersToKilometers(distanceBetweenAmsterdamAndUtrecht));
function distanceBetweenStationsInMeters(station1, station2) {
return distanceInMeters(station1.coordinates, station2.coordinates);
}

function departure(route) {
return route[0];
}

//["Amsterdam", "Utrecht", "Arnhem"]
//console.log('departure: ', departure(["Amsterdam", "Utrecht", "Arnhem"]));
function destination(route) {
return route[route.length - 1];
}

//console.log('destination: ', destination(["Amsterdam", "Utrecht", "Arnhem"]));
function isInRoute(route, city) {
return route.includes(city);
}
Expand All @@ -83,18 +94,21 @@ function updateRoute(route, city) {
}

function routes(departingCity, destinationCity) {
if(departingCity === destinationCity) {
throw "Destination city cannot be the same as departure city."
if (departingCity === destinationCity) {
throw "Destination city cannot be the same as departure city.";
}

const possibleRoutes = [];
function buildRoutes(route) {
const currentCity = route[route.length - 1];
//console.log('currentCity: ', currentCity);
const currentStation = trainStations[currentCity];
//console.log('currentStation: ', currentStation);
currentStation.connections.forEach(connectedCity => {
if(!isInRoute(route, connectedCity)) {
if (!isInRoute(route, connectedCity)) {
const updatedRoute = updateRoute(route, connectedCity);
if(connectedCity === destinationCity) {
//console.log('updatedRoute: ', updatedRoute);
if (connectedCity === destinationCity) {
// Stop the presses! We have a route!
possibleRoutes.push(updatedRoute);
} else {
Expand All @@ -110,30 +124,35 @@ function routes(departingCity, destinationCity) {


function routeLengthInKilometers(route) {
if(route.length < 2) {
if (route.length < 2) {
return 0;
} else {
let totalLengthInKm = 0;
for(let index = 0; index < route.length - 1; index++) {
for (let index = 0; index < route.length - 1; index++) {
const currentCity = route[index];
const currentStation = trainStations[currentCity];
const nextCity = route[index + 1];
const nextStation = trainStations[nextCity];
totalLength = totalLengthInKm + distanceBetweenStationsInMeters(currentStation, nextStation);
totalLength = metersToKilometers(totalLengthInKm);
const distanceInMeter = distanceBetweenStationsInMeters(currentStation, nextStation);
const distanceInKm = metersToKilometers(distanceInMeter);
totalLengthInKm += distanceInKm;
}
return totalLengthInKm;
}
}

function shortestRoute(routes) {
if(routes.length === 0) {
throw "Have to provide at least one route"
//console.log(routes);
if (routes.length === 0) {
throw "Have to provide at least one route";
} else {
const currentShortestRoute = routes[0];
for(let index = 1; index < routes.length - 1; index++) {
let currentShortestRoute = routes[0];
//console.log('current Shortest Route is: ', currentShortestRoute);
for (let index = 1; index < routes.length - 1; index++) {
const route = routes[index];
if(routeLengthInKilometers(currentShortestRoute) < routeLengthInKilometers(route)) {
//console.log(route);
//console.log(routeLengthInKilometers(currentShortestRoute));
if (routeLengthInKilometers(currentShortestRoute) > routeLengthInKilometers(route)) {
currentShortestRoute = route;
}
}
Expand All @@ -143,16 +162,20 @@ function shortestRoute(routes) {

function routeCostInEuros(route) {
const pricePerKilometers = 0.19;
routeLengthInKilometers(route) * pricePerKilometers;
const routeLengthInKm = routeLengthInKilometers(route);
const price = routeLengthInKm * pricePerKilometers;
// We have to return the result, or will give us undefined
return price;
//console.log('€',price);
}

const amsterdamArnhemRoutes = routes("Amsterdam", "Arnhem");
console.log(amsterdamArnhemRoutes);
//console.log(amsterdamArnhemRoutes);
const shortestAmsterdamArnhemRoute = shortestRoute(amsterdamArnhemRoutes);

//console.log(shortestAmsterdamArnhemRoute);
console.log(
"Our route:",
shortestAmsterdamArnhemRoute,
"is",
routeLengthInKilometers(shortestAmsterdamArnhemRoute) + " km",
", costing €" + routeCostInEuros(shortestAmsterdamArnhemRoute));
", costing € " + routeCostInEuros(shortestAmsterdamArnhemRoute));