FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptExercises/shuffle.js at master · marshmeryl/JavaScriptExercises · GitHub
marshmeryl
/
JavaScriptExercises
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptExercises
/
shuffle.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (35 loc) · 1.08 KB
Breadcrumbs
JavaScriptExercises
/
shuffle.js
Copy path
File metadata and controls
42 lines (35 loc) · 1.08 KB
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
40
41
42
function
shuffle
(
array
)
{
var
result
=
array
.
slice
(
)
;
var
length
=
result
.
length
-
1
;
var
randIndex
;
for
(
var
i
=
length
;
i
>
0
;
i
--
)
{
randIndex
=
Math
.
floor
(
(
i
+
1
)
*
Math
.
random
(
)
)
;
_swap
(
result
,
i
,
randIndex
)
;
}
return
result
;
}
function
_swap
(
arr
,
indexOne
,
indexTwo
)
{
var
temp
=
arr
[
indexOne
]
;
arr
[
indexOne
]
=
arr
[
indexTwo
]
;
arr
[
indexTwo
]
=
temp
;
}
var
testCase
=
[
[
]
,
[
1
]
,
[
2
,
1
]
,
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
,
[
10
,
9
,
8
,
7
,
6
,
5
,
4
,
3
,
2
,
1
]
]
;
for
(
var
i
=
0
;
i
<
testCase
.
length
;
i
++
)
{
if
(
testCase
[
i
]
.
length
==
0
)
{
console
.
log
(
"Empty array"
)
;
}
else
{
console
.
log
(
"================================================="
)
;
console
.
log
(
" Original array: "
+
testCase
[
i
]
.
join
(
", "
)
)
;
console
.
log
(
"Randomized array 1x: "
+
shuffle
(
testCase
[
i
]
)
.
join
(
", "
)
)
;
console
.
log
(
"Randomized array 2x: "
+
shuffle
(
testCase
[
i
]
)
.
join
(
", "
)
)
;
console
.
log
(
"Randomized array 3x: "
+
shuffle
(
testCase
[
i
]
)
.
join
(
", "
)
)
;
console
.
log
(
"Randomized array 4x: "
+
shuffle
(
testCase
[
i
]
)
.
join
(
", "
)
)
;
console
.
log
(
"Randomized array 5x: "
+
shuffle
(
testCase
[
i
]
)
.
join
(
", "
)
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL