Skip to content

added GridGetX and GridGetY #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions Maths/GridGetX.js
Original file line number Diff line number Diff line change
@@ -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)}`)
17 changes: 17 additions & 0 deletions Maths/GridGetY.js
Original file line number Diff line number Diff line change
@@ -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)}`)