FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptAlgorithm/String/ReverseString.js at master · RabibHossain/JavaScriptAlgorithm · GitHub
RabibHossain
/
JavaScriptAlgorithm
Public
forked from
TheAlgorithms/JavaScript
Notifications
You must be signed in to change notification settings
Fork
1
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptAlgorithm
/
String
/
ReverseString.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (32 loc) · 929 Bytes
Breadcrumbs
JavaScriptAlgorithm
/
String
/
ReverseString.js
Copy path
File metadata and controls
39 lines (32 loc) · 929 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* A short example showing how to reverse a string.
*/
function
ReverseStringIterative
(
string
)
{
if
(
typeof
string
!==
'string'
)
{
throw
new
TypeError
(
'The given value is not a string'
)
}
let
reversedString
=
''
let
index
for
(
index
=
string
.
length
-
1
;
index
>=
0
;
index
--
)
{
reversedString
+=
string
[
index
]
}
return
reversedString
}
/**
* JS disallows string mutation so we're actually a bit slower.
*
*
@complexity
O(n)
*/
function
ReverseStringIterativeInplace
(
string
)
{
if
(
typeof
string
!==
'string'
)
{
throw
new
TypeError
(
'The given value is not a string'
)
}
const
_string
=
string
.
split
(
''
)
for
(
let
i
=
0
;
i
<
Math
.
floor
(
_string
.
length
/
2
)
;
i
++
)
{
const
first
=
_string
[
i
]
_string
[
i
]
=
_string
[
_string
.
length
-
1
-
i
]
_string
[
_string
.
length
-
1
-
i
]
=
first
}
return
_string
.
join
(
''
)
}
export
{
ReverseStringIterative
,
ReverseStringIterativeInplace
}
Back
|
FazBrowse Home
|
New Git URL