JSFiddle - React, Tailwind, and code Playground
by lavisha99
JavaScript
/*Narrative: You are asked to enable landmark objects to calculate the distance to other landmark objects. You need to use the Manhattan distance for calculating the distance. The formula for calculating the Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2) where abs means absolute value. Hint: The Math object has all the methods you need for calculating the Manhattan distance.
Constraints:
IMPORTANT: Don't forget to write a set of instructions before start coding!
You must implement this function as a method of the landmark constructor. This method should receive a landmark object as a parameter and return a number representing the distance between the received landmark and the current landmark object.*/
//INSTRUCTIONS
/*
1. create a landmark map with all the locaton details of name, X coordinate and Y coordinate
2. create a new property named this.distance that calculates the distance using bs(x1 - x2) + abs(y1 - y2).
3. within this property, create a function that calculates this distance.
4. within this function, create a for loop that iterates through all the landmarks
5. within this loop, create another for loop that iterates through the values in the landmarks of the array
6. create a variable that stores the distance
7. put in the distance calculation code for the variable
8. log the distance
*/
function landmark(name, Xcordinate, Ycordinate) {
this.name = name;
this.Xcordinate = Xcordinate;
this.Ycordinate = Ycordinate;
this.distance = function() { //creating a function that calculates distance between two points
for (i = 0; i < locations.length; i++) { // iterating through the length of the locations array
for (j = 0; j < locations[i]; j++) { //iterating through each new landmark in the location array
let mdistance = abs(locations[[j] - 1].Xcordinate - locations[j].Xcordinate) + abs(locations[[j] - 1].Ycordinate - locations[j].Ycordinate)
}
}
return mdistance;
...