aboutsummaryrefslogtreecommitdiff
path: root/challenge-152/roger-bell-west/javascript/ch-2.js
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-152/roger-bell-west/javascript/ch-2.js')
-rwxr-xr-xchallenge-152/roger-bell-west/javascript/ch-2.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-152/roger-bell-west/javascript/ch-2.js b/challenge-152/roger-bell-west/javascript/ch-2.js
new file mode 100755
index 0000000000..6a97ee21b0
--- /dev/null
+++ b/challenge-152/roger-bell-west/javascript/ch-2.js
@@ -0,0 +1,48 @@
+#! /usr/bin/node
+
+function Rect(xy1,xy2) {
+ let r=Object.create(Rect.methods);
+ r.xy1=[Math.min(xy1[0],xy2[0]),Math.min(xy1[1],xy2[1])];
+ r.xy2=[Math.max(xy1[0],xy2[0]),Math.max(xy1[1],xy2[1])];
+ return r;
+}
+
+Rect.methods = {
+ area() {
+ let area=1;
+ for (let axis=0;axis<=1;axis++) {
+ area *= this.xy2[axis]-this.xy1[axis];
+ }
+ return area;
+ },
+
+ overlap(other) {
+ let area=1;
+ for (let axis=0;axis<=1;axis++) {
+ area *= Math.max(0,
+ Math.min(this.xy2[axis],other.xy2[axis])-
+ Math.max(this.xy1[axis],other.xy1[axis])
+ );
+ }
+ return area;
+ },
+
+ fullarea(other) {
+ return this.area()+other.area()-this.overlap(other);
+ }
+}
+
+if (new Rect([-1,0],[2,2]).fullarea(new Rect([0,-1],[4,4])) == 22) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write(" ");
+
+if (new Rect([-3,-1],[1,3]).fullarea(new Rect([-1,-3],[2,2])) == 25) {
+ process.stdout.write("Pass");
+} else {
+ process.stdout.write("FAIL");
+}
+process.stdout.write("\n");
+