Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit636017c

Browse files
authored
algorithm: reverse (TheAlgorithms#1197)
1 parentb07529f commit636017c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

‎Data-Structures/Array/Reverse.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** https://www.geeksforgeeks.org/write-a-program-to-Reverse-an-array-or-string/
2+
* This function will accept an array and
3+
* Reverse its elements and returns the inverted array
4+
*@param {Array} arr array with elements of any data type
5+
*@returns {Array} array with inverted elements
6+
*/
7+
8+
constReverse=(arr)=>{
9+
// limit specifies the amount of Reverse actions
10+
for(leti=0,j=arr.length-1;i<arr.length/2;i++,j--){
11+
consttemp=arr[i]
12+
arr[i]=arr[j]
13+
arr[j]=temp
14+
}
15+
returnarr
16+
}
17+
export{Reverse}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import{Reverse}from'../Reverse.js'
2+
importeachfrom'jest-each'
3+
4+
describe('reverse elements in an array',()=>{
5+
each`
6+
array | expected
7+
${[]} |${[]}
8+
${[1]} |${[1]}
9+
${[1,2,3,4]} |${[4,3,2,1]}
10+
`.test('returns $expected when given $array',({ array, expected})=>{
11+
expect(Reverse(array)).toEqual(expected)
12+
})
13+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp