From a0c188987e76a833e79b951f95c0bc6ae2799e71 Mon Sep 17 00:00:00 2001 From: tangibledream Date: Sat, 4 Jul 2020 12:44:30 -0500 Subject: [PATCH 1/5] added GridGetX and GridGetY --- DIRECTORY.md | 2 ++ Maths/GridGetX.js | 20 ++++++++++++++++++++ Maths/GridGetY.js | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 Maths/GridGetX.js create mode 100644 Maths/GridGetY.js diff --git a/DIRECTORY.md b/DIRECTORY.md index ce78317191..d191a79fe6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -68,6 +68,8 @@ * [Fibonacci](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Fibonacci.js) * [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js) * [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js) + * [GridGetX](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGetX.js) + * [GridGetY](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGetY.js) * [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js) * [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js) * [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PiApproximationMonteCarlo.js) diff --git a/Maths/GridGetX.js b/Maths/GridGetX.js new file mode 100644 index 0000000000..c710bd423a --- /dev/null +++ b/Maths/GridGetX.js @@ -0,0 +1,20 @@ +/* + author: TangibleDream + license: GPL-3.0 or later + + This script will find x given the element and columns for a 2 dimensional array. + + If your array is a perfect square, you can find columns by getting the square + root of the length of the array. + +*/ + +const gridGetX = (columns, index) => { + while ((index + 1) > columns) { + index = index - columns; + } + return (index + 1); +} + +console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400),27)}`); +console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7,11)}`); \ No newline at end of file diff --git a/Maths/GridGetY.js b/Maths/GridGetY.js new file mode 100644 index 0000000000..a38f3724d8 --- /dev/null +++ b/Maths/GridGetY.js @@ -0,0 +1,17 @@ +/* + author: TangibleDream + license: GPL-3.0 or later + + This script will find y given the element and columns for a 2 dimensional array. + + If your array is a perfect square, you can find columns by getting the square + root of the length of the array. + +*/ + +const gridGetY = (columns, index) => { + return (Math.floor(index/columns)) + 1; +} + +console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400),27)}`); +console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7,11)}`); \ No newline at end of file From 31dd535552f3403046b6a8d35e3b79f66a555f26 Mon Sep 17 00:00:00 2001 From: tangibledream Date: Sat, 4 Jul 2020 14:04:42 -0500 Subject: [PATCH 2/5] fixed Node CI issues --- Maths/GridGetX.js | 10 +++++----- Maths/GridGetY.js | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Maths/GridGetX.js b/Maths/GridGetX.js index c710bd423a..905c02b844 100644 --- a/Maths/GridGetX.js +++ b/Maths/GridGetX.js @@ -10,11 +10,11 @@ */ const gridGetX = (columns, index) => { - while ((index + 1) > columns) { - index = index - columns; + while ((index + 1) > columns) { + index = index - columns } - return (index + 1); + return (index + 1); } -console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400),27)}`); -console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7,11)}`); \ No newline at end of file +console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400),27)}`); +console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7,11)}`); diff --git a/Maths/GridGetY.js b/Maths/GridGetY.js index a38f3724d8..c0b62dd005 100644 --- a/Maths/GridGetY.js +++ b/Maths/GridGetY.js @@ -10,8 +10,8 @@ */ const gridGetY = (columns, index) => { - return (Math.floor(index/columns)) + 1; + return (Math.floor(index / columns)) + 1 } -console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400),27)}`); -console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7,11)}`); \ No newline at end of file +console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400),27)}`); +console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7,11)}`); From 5c282836ce10426af6be603e2565b7a2cd9ec0e6 Mon Sep 17 00:00:00 2001 From: tangibledream Date: Sat, 4 Jul 2020 14:13:43 -0500 Subject: [PATCH 3/5] fixed Node CI issues --- Maths/GridGetX.js | 8 ++++---- Maths/GridGetY.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Maths/GridGetX.js b/Maths/GridGetX.js index 905c02b844..1c98899768 100644 --- a/Maths/GridGetX.js +++ b/Maths/GridGetX.js @@ -12,9 +12,9 @@ const gridGetX = (columns, index) => { while ((index + 1) > columns) { index = index - columns - } - return (index + 1); + } + return (index + 1) } -console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400),27)}`); -console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7,11)}`); +console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400), 27)}`) +console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7, 11)}`) diff --git a/Maths/GridGetY.js b/Maths/GridGetY.js index c0b62dd005..748ca7f52f 100644 --- a/Maths/GridGetY.js +++ b/Maths/GridGetY.js @@ -13,5 +13,5 @@ const gridGetY = (columns, index) => { return (Math.floor(index / columns)) + 1 } -console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400),27)}`); -console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7,11)}`); +console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400), 27)}`) +console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7, 11)}`) From b73337d5d7dfdd9a3440022c72bac4b1a8222828 Mon Sep 17 00:00:00 2001 From: tangibledream Date: Mon, 6 Jul 2020 19:52:53 -0500 Subject: [PATCH 4/5] made requested changes --- DIRECTORY.md | 2 -- Maths/GridGet.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++ Maths/GridGetX.js | 20 ---------------- Maths/GridGetY.js | 17 -------------- 4 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 Maths/GridGet.js delete mode 100644 Maths/GridGetX.js delete mode 100644 Maths/GridGetY.js diff --git a/DIRECTORY.md b/DIRECTORY.md index d191a79fe6..ce78317191 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -68,8 +68,6 @@ * [Fibonacci](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Fibonacci.js) * [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js) * [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js) - * [GridGetX](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGetX.js) - * [GridGetY](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGetY.js) * [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js) * [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js) * [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PiApproximationMonteCarlo.js) diff --git a/Maths/GridGet.js b/Maths/GridGet.js new file mode 100644 index 0000000000..9222e7ea54 --- /dev/null +++ b/Maths/GridGet.js @@ -0,0 +1,58 @@ +/* + author: TangibleDream + license: GPL-3.0 or later + + These methods will find x or y given the element and columns for a 2 dimensional array. + + If your array is a perfect square, you can find columns by getting the square + root of the length of the array. + + Let's say for instance you had an array of 10 by 10 or 100, elements and you wanted to + find the shortest distance between element 3, and element 49. In this case coding out + a function to return the distance without finding x and y for both elements I found to + be painful. If you first find x and y, where 3 becomes 4,1 and 49 becomes 10,5, you can + find distance by first subtracting x from x and y from y this returns 6,4 or -6,-4. + Next apply absolute value to assure the results are positive, + last choose the maximum value of the set, or 6. + + +--+--+--+--+--+--+--+--+--+--+ + | | | | 3| | | | | | | + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | | | | | | + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | | | | | | + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | | | | |49| + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | | | | | | + + + +--+--+--+--+--+--+--+--+--+--+ + | | | | 3| | | | | | | + +--+--+--+--+--+--+--+--+--+--+ + | | | | | 1| | | | | | + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | 2| | | | | + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | | 3| 4| 5|6!| + +--+--+--+--+--+--+--+--+--+--+ + | | | | | | | | | | | + + +*/ + +const gridGetX = (columns, index) => { + while ((index + 1) > columns) { + index = index - columns + } + return (index + 1) +} + +const gridGetY = (columns, index) => { + return (Math.floor(index / columns)) + 1 +} + +console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400), 27)}`) +console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7, 11)}`) +console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400), 27)}`) +console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7, 11)}`) diff --git a/Maths/GridGetX.js b/Maths/GridGetX.js deleted file mode 100644 index 1c98899768..0000000000 --- a/Maths/GridGetX.js +++ /dev/null @@ -1,20 +0,0 @@ -/* - author: TangibleDream - license: GPL-3.0 or later - - This script will find x given the element and columns for a 2 dimensional array. - - If your array is a perfect square, you can find columns by getting the square - root of the length of the array. - -*/ - -const gridGetX = (columns, index) => { - while ((index + 1) > columns) { - index = index - columns - } - return (index + 1) -} - -console.log(`If a square array has 400 elements, then the value of x for the 27th element is ${gridGetX(Math.sqrt(400), 27)}`) -console.log(`If an array has 7 columns and 3 rows, then the value of x for the 11th element is ${gridGetX(7, 11)}`) diff --git a/Maths/GridGetY.js b/Maths/GridGetY.js deleted file mode 100644 index 748ca7f52f..0000000000 --- a/Maths/GridGetY.js +++ /dev/null @@ -1,17 +0,0 @@ -/* - author: TangibleDream - license: GPL-3.0 or later - - This script will find y given the element and columns for a 2 dimensional array. - - If your array is a perfect square, you can find columns by getting the square - root of the length of the array. - -*/ - -const gridGetY = (columns, index) => { - return (Math.floor(index / columns)) + 1 -} - -console.log(`If a square array has 400 elements, then the value of y for the 27th element is ${gridGetY(Math.sqrt(400), 27)}`) -console.log(`If an array has 7 columns and 3 rows, then the value of y for the 11th element is ${gridGetY(7, 11)}`) From 75223f7afebb32d87a2e9885fef95bb6db34e13c Mon Sep 17 00:00:00 2001 From: tangibledream Date: Mon, 6 Jul 2020 19:56:12 -0500 Subject: [PATCH 5/5] fixed Node CI errors --- Maths/GridGet.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/Maths/GridGet.js b/Maths/GridGet.js index 9222e7ea54..69ae502efd 100644 --- a/Maths/GridGet.js +++ b/Maths/GridGet.js @@ -26,7 +26,6 @@ +--+--+--+--+--+--+--+--+--+--+ | | | | | | | | | | | - +--+--+--+--+--+--+--+--+--+--+ | | | | 3| | | | | | | +--+--+--+--+--+--+--+--+--+--+ @@ -38,7 +37,6 @@ +--+--+--+--+--+--+--+--+--+--+ | | | | | | | | | | | - */ const gridGetX = (columns, index) => {