Overriding Prototypal Methods I

The cowboy-devs aren’t having much luck with the native valueOf() property provided by the Object prototype. Currently, they get this long list of properties, but what they’d really like to know is how far the post is from the ranch (0,0) as the crow flies.

Your job is to override the valueOf() function using the Fencepost prototype to produce the exact distance a post is away from the ranch.

Here’s the standard algebraic distance formula:

straight line distance = square root of [ (x1 - x2)^2 + (y1- y2)^2 ]

This formula includes points (x1, y1) for the post, and (x2, y2) for the ranch.

Note: Since the ranch’s coordinates are always (0,0), you should be able to shorten this procedure considerably, or at the very least hard-code some values. But think carefully about what the ranch’s zero coordinates might do to make this an easier formula. One with no subtraction at all…

So that you can accomplish this in code, the cowboy-devs have sent over an EXTRA tool that you didn’t see in the videos…sneaky, huh? Behold! The sqrt() method of the Math class!

Math.sqrt(49);      // 7
Math.sqrt(45+55);   // 10
Math.sqrt(30/6);    // 2.23606797749979
Math.sqrt("YOLO")   // Don't be ridiculous.
You can place any numerical expression you like as the parameter for the Math.sqrt() method, and it will take the square root after evaluating the expression. Use this method carefully in your override, and feel free to test it in your own console if you need to. Good luck, you budding Pythagoras!
Overriding Prototypal Methods II

Next up, the Plains of Prototypes cowboy-devs have asked for an improved toString method for their fence posts. An example of the string message they’d like is below:

Fence post #10:
Connected to posts:
11
12
13
Distance from ranch: 5 yards
Override the toString method by using the Fencepost prototype and a newly built function that returns a string message in the above format, but with unique data for each post. The constructor and all of your previous prototype additions are provided for you as a reference for property names and procedures. Remember that connectionsTo is a list of Fencepost objects.