Skip to content
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

Upadated reverse array code #1331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
28 changes: 13 additions & 15 deletions Data-Structures/Array/Reverse.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/** https://www.geeksforgeeks.org/write-a-program-to-Reverse-an-array-or-string/
* This function will accept an array and
* Reverse its elements and returns the inverted array
* @param {Array} arr array with elements of any data type
* @returns {Array} array with inverted elements
/**
* This function accepts an array and reverses its elements.
* @param {Array} arr - Array with elements of any data type.
* @returns {Array} - Array with reversed elements.
*/

const Reverse = (arr) => {
// limit specifies the amount of Reverse actions
for (let i = 0, j = arr.length - 1; i < arr.length / 2; i++, j--) {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
const reverseArray = (arr) => {
for (let i = 0, j = arr.length - 1; i < j; i++, j--) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr
}
export { Reverse }
return arr;
};

export default reverseArray;