From 480eb7fc86ff8ee67080ed57b2db08bc002e14ab Mon Sep 17 00:00:00 2001 From: Gary Baker Date: Thu, 15 Nov 2018 08:49:04 -0700 Subject: [PATCH 1/4] Added Midpoint.js --- snippets/midpoint.md | 24 ++++++++++++++++++++++++ tag_database | 1 + test/midpoint.test.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 snippets/midpoint.md create mode 100644 test/midpoint.test.js diff --git a/snippets/midpoint.md b/snippets/midpoint.md new file mode 100644 index 000000000..f38a509af --- /dev/null +++ b/snippets/midpoint.md @@ -0,0 +1,24 @@ +### midpoint + +Calculates the midpoint between two pairs of x and y points. + +Takes in two pairs of (x,y) points, calculates the midpoint between the x's, the midpoint between the y's, and returns the coordinates in an array. + +```js +const midpoint = (x1, y1, x2, y2) => { + let points = new Array(2); + let first_point = (x1 + x2) / 2; + let second_point = (y1 + y2) / 2; + + points[0] = first_point; + points[1] = second_point; + return points; +}; +``` + +```js +midpoint(2, 2, 4, 4); // [3, 3] +midpoint(4, 4, 6, 6); // [5, 5] +midpoint(1, 3, 2, 4); // [1.5, 3.5] +``` + diff --git a/tag_database b/tag_database index aaa61a47e..1c6355db8 100644 --- a/tag_database +++ b/tag_database @@ -186,6 +186,7 @@ maxN:array,math,beginner median:math,array,intermediate memoize:function,advanced merge:object,array,intermediate +midpoint:math,array,beginner minBy:math,array,function,beginner minDate:date,math,beginner minN:array,math,beginner diff --git a/test/midpoint.test.js b/test/midpoint.test.js new file mode 100644 index 000000000..423301a08 --- /dev/null +++ b/test/midpoint.test.js @@ -0,0 +1,35 @@ +const expect = require('expect'); +const {midpoint} = require('./_30s.js'); + +test('midpoint is a Function', () => { + expect(midpoint).toBeInstanceOf(Function); +}); + +test('midpoint(2, 2, 4, 4) = [3, 3]', () => { + expect(midpoint(2, 2, 4, 4)).toEqual([3, 3]) +}); + +test('midpoint(4, 4, 6, 6) = [5,5]', () => { + expect(midpoint(4, 4, 6, 6)).toEqual([5, 5]) +}); + +test('midpoint(1, 3, 2, 4) = [3,3]', () => { + expect(midpoint(1, 3, 2, 4)).toEqual([1.5, 3.5]) +}); + +test('midpoint(-2, 0, -2, 4) = [-2,2]', () => { + expect(midpoint(-2, 0, -2, 4)).toEqual([-2, 2]) +}); + +test('midpoint(0, 0, 0, 0) = [0,0]', () => { + expect(midpoint(0, 0, 0, 0)).toEqual([0, 0]) +}); + +test('midpoint(10, -10, 20, 30) = [15, 10]', () => { + expect(midpoint(10, -10, 20, 30)).toEqual([15, 10]) +}); + +test('midpoint(7.5, -1.3, 2.7, 11.1) = [5.1, 4.8999999999999995]', () => { + expect(midpoint(7.5, -1.3, 2.7, 11.1)).toEqual([5.1, 4.8999999999999995]) +}); + From f8a8bd6ea093c292cc2f7671436bb3e67388aa89 Mon Sep 17 00:00:00 2001 From: Gary Baker Date: Thu, 15 Nov 2018 17:30:12 -0700 Subject: [PATCH 2/4] Added missing semicolons to Midpoint.js --- test/midpoint.test.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/midpoint.test.js b/test/midpoint.test.js index 423301a08..74e37e360 100644 --- a/test/midpoint.test.js +++ b/test/midpoint.test.js @@ -6,30 +6,29 @@ test('midpoint is a Function', () => { }); test('midpoint(2, 2, 4, 4) = [3, 3]', () => { - expect(midpoint(2, 2, 4, 4)).toEqual([3, 3]) + expect(midpoint(2, 2, 4, 4)).toEqual([3, 3]); }); test('midpoint(4, 4, 6, 6) = [5,5]', () => { - expect(midpoint(4, 4, 6, 6)).toEqual([5, 5]) + expect(midpoint(4, 4, 6, 6)).toEqual([5, 5]); }); test('midpoint(1, 3, 2, 4) = [3,3]', () => { - expect(midpoint(1, 3, 2, 4)).toEqual([1.5, 3.5]) + expect(midpoint(1, 3, 2, 4)).toEqual([1.5, 3.5]); }); test('midpoint(-2, 0, -2, 4) = [-2,2]', () => { - expect(midpoint(-2, 0, -2, 4)).toEqual([-2, 2]) + expect(midpoint(-2, 0, -2, 4)).toEqual([-2, 2]); }); test('midpoint(0, 0, 0, 0) = [0,0]', () => { - expect(midpoint(0, 0, 0, 0)).toEqual([0, 0]) + expect(midpoint(0, 0, 0, 0)).toEqual([0, 0]); }); test('midpoint(10, -10, 20, 30) = [15, 10]', () => { - expect(midpoint(10, -10, 20, 30)).toEqual([15, 10]) + expect(midpoint(10, -10, 20, 30)).toEqual([15, 10]); }); test('midpoint(7.5, -1.3, 2.7, 11.1) = [5.1, 4.8999999999999995]', () => { - expect(midpoint(7.5, -1.3, 2.7, 11.1)).toEqual([5.1, 4.8999999999999995]) + expect(midpoint(7.5, -1.3, 2.7, 11.1)).toEqual([5.1, 4.8999999999999995]); }); - From 26833c264cc8e943f56b70a6a485a15e040b4ba2 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 7 Dec 2018 18:23:29 +0200 Subject: [PATCH 3/4] Update midpoint.md --- snippets/midpoint.md | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/snippets/midpoint.md b/snippets/midpoint.md index f38a509af..05f5dcf8f 100644 --- a/snippets/midpoint.md +++ b/snippets/midpoint.md @@ -1,24 +1,16 @@ ### midpoint -Calculates the midpoint between two pairs of x and y points. +Calculates the midpoint between two pairs of (x,y) points. -Takes in two pairs of (x,y) points, calculates the midpoint between the x's, the midpoint between the y's, and returns the coordinates in an array. +Destructure the array to get `x1`, `y1`, `x2` and `y2`, calculate the midpoint for each dimension by dividing the sum of the two endpoints by `2`. ```js -const midpoint = (x1, y1, x2, y2) => { - let points = new Array(2); - let first_point = (x1 + x2) / 2; - let second_point = (y1 + y2) / 2; - - points[0] = first_point; - points[1] = second_point; - return points; -}; +const midpoint = ([x1, y1], [x2, y2]) => [(x1 + x2) / 2, (y1 + y2) / 2]; ``` ```js -midpoint(2, 2, 4, 4); // [3, 3] -midpoint(4, 4, 6, 6); // [5, 5] -midpoint(1, 3, 2, 4); // [1.5, 3.5] +midpoint([2, 2], [4, 4]); // [3, 3] +midpoint([4, 4], [6, 6]); // [5, 5] +midpoint([1, 3], [2, 4]); // [1.5, 3.5] ``` From f4a035b5133d3cdbdb7d2abf2f078b7e27334ad3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 7 Dec 2018 18:24:50 +0200 Subject: [PATCH 4/4] Update midpoint.test.js --- test/midpoint.test.js | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/midpoint.test.js b/test/midpoint.test.js index 74e37e360..7926f2a50 100644 --- a/test/midpoint.test.js +++ b/test/midpoint.test.js @@ -1,34 +1,34 @@ const expect = require('expect'); -const {midpoint} = require('./_30s.js'); +const { midpoint } = require('./_30s.js'); test('midpoint is a Function', () => { expect(midpoint).toBeInstanceOf(Function); }); -test('midpoint(2, 2, 4, 4) = [3, 3]', () => { - expect(midpoint(2, 2, 4, 4)).toEqual([3, 3]); +test('midpoint([2, 2], [4, 4]) = [3, 3]', () => { + expect(midpoint([2, 2], [4, 4])).toEqual([3, 3]); }); -test('midpoint(4, 4, 6, 6) = [5,5]', () => { - expect(midpoint(4, 4, 6, 6)).toEqual([5, 5]); +test('midpoint([4, 4], [6, 6]) = [5,5]', () => { + expect(midpoint([4, 4], [6, 6])).toEqual([5, 5]); }); -test('midpoint(1, 3, 2, 4) = [3,3]', () => { - expect(midpoint(1, 3, 2, 4)).toEqual([1.5, 3.5]); +test('midpoint([1, 3], [2, 4]) = [3,3]', () => { + expect(midpoint([1, 3], [2, 4])).toEqual([1.5, 3.5]); }); -test('midpoint(-2, 0, -2, 4) = [-2,2]', () => { - expect(midpoint(-2, 0, -2, 4)).toEqual([-2, 2]); +test('midpoint([-2, 0], [-2, 4]) = [-2,2]', () => { + expect(midpoint([-2, 0], [-2, 4])).toEqual([-2, 2]); }); -test('midpoint(0, 0, 0, 0) = [0,0]', () => { - expect(midpoint(0, 0, 0, 0)).toEqual([0, 0]); +test('midpoint([0, 0], [0, 0]) = [0,0]', () => { + expect(midpoint([0, 0], [0, 0])).toEqual([0, 0]); }); -test('midpoint(10, -10, 20, 30) = [15, 10]', () => { - expect(midpoint(10, -10, 20, 30)).toEqual([15, 10]); +test('midpoint([10, -10], [20, 30]) = [15, 10]', () => { + expect(midpoint([10, -10], [20, 30])).toEqual([15, 10]); }); -test('midpoint(7.5, -1.3, 2.7, 11.1) = [5.1, 4.8999999999999995]', () => { - expect(midpoint(7.5, -1.3, 2.7, 11.1)).toEqual([5.1, 4.8999999999999995]); +test('midpoint([7.5, -1.3], [2.7, 11.1]) = [5.1, 4.8999999999999995]', () => { + expect(midpoint([7.5, -1.3], [2.7, 11.1])).toEqual([5.1, 4.8999999999999995]); });