JSFiddle - React, Tailwind, and code Playground
by lavisha99
JavaScript
/** PROBLEM 1 **/
// You must implement this function as a method of the landmark constructor <-- recieve landmark object
// Landmark
// has x, y, name
function Landmark(x, y, name) {
this.x = x;
this.y = y;
this.name = name;
/* Function that calculates the distance between two landmarks using the Manhattan distance.
* @params - Landmark object <- the other object to find the distance to.
* @returns - number <- the distance between the objects.
*/
this.findDistance = function(landmark) {
// abs(x1 - x2) + abs(y1 - y2)
// x1 is the current landmarks x location.
let x1 = this.x;
// x2 is the other landmarks x location.
let x2 = landmark.x;
// y1 is the current landmarks y location.
let y1 = this.y;
// y2 is the other landmarks y location.
let y2 = landmark.y
let distance = Math.abs(x1 - x2) + Math.abs(y1 - y2);
// return the distance
return distance;
}
/* this.findDistanceOneLine = function(landmark) {
// abs(x1 - x2) + abs(y1 - y2)
// x1 is the current landmarks x location.
// x2 is the other landmarks x location.
// y1 is the current landmarks y location.
// y2 is the other landmarks y location.
return Math.abs(this.x - landmark.x) + Math.abs(this.y - landmark.y);
} */
}
let skytower = new Landmark(9, 40, "Sky Tower");
let museum = new Landmark(10, 10, "Museum");
let beach = new Landmark(10, 800, "beach");
let myloc = new Landmark(9, 40, 'myloc');
// expect distance to be 20.
let distance = skytower.findDistance(museum);
//nsole.log(distance);
// now i have the distance from any point to another point
// i create an array of all the landmarks with their co ordinates
// i want to be able to calculate the distance from any point to all other points in the array
// i want to loop through all other points in the array and compare its distance with the given point
// i want it to loop through the distances returned and give me the name of the point...