From 23578815a9ccd5c0d9f7e1bcbdfafbb2e58337db Mon Sep 17 00:00:00 2001 From: yousefallaban Date: Mon, 15 Jan 2018 14:07:22 +0100 Subject: [PATCH] I made my homework fix all debugging --- .../homework/train-stations-complete.js | 95 ++++++++++++------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/Debugging2/homework/train-stations-complete.js b/Debugging2/homework/train-stations-complete.js index c73a7f9..0e30d5b 100644 --- a/Debugging2/homework/train-stations-complete.js +++ b/Debugging2/homework/train-stations-complete.js @@ -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) { @@ -47,19 +56,20 @@ 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); } @@ -67,11 +77,12 @@ function distanceBetweenStationsInMeters(station1, station2) { 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); } @@ -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 { @@ -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; } } @@ -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));