teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

.

2019/11/15 05:18

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  for (i = min; i <= max ; i++){
10
10
  while(true){
11
- let tmp = getRandom(min,max);
11
+ let tmp = getRandom(min,max);
12
12
  if(!randoms.includes(tmp)){
13
13
  randoms.push(tmp);
14
14
  break;
@@ -39,7 +39,7 @@
39
39
 
40
40
  for (i = min; i <= max ; i++){
41
41
  while(true){
42
- let tmp = getRandom(min,max);
42
+ let tmp = getRandom(min,max);
43
43
  if(!randoms.includes(tmp)){
44
44
  randoms.push(tmp);
45
45
  break;

2

.

2019/11/15 05:18

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -3,15 +3,15 @@
3
3
  let min = 1,max = 10;
4
4
 
5
5
  function getRandom(min,max){
6
- return Math.floor(Math.random()*(max - min +1)+min);
6
+ return Math.floor(Math.random()*(max - min +1)+min);
7
7
  }
8
8
 
9
9
  for (i = min; i <= max ; i++){
10
10
  while(true){
11
11
  let tmp = getRandom(min,max);
12
12
  if(!randoms.includes(tmp)){
13
- randoms.push(tmp);
13
+ randoms.push(tmp);
14
- break;
14
+ break;
15
15
  }
16
16
  }
17
17
  }
@@ -34,15 +34,15 @@
34
34
  let min = 1,max = 10;
35
35
 
36
36
  let getRandom = function(min,max){
37
- return Math.floor(Math.random()*(max - min +1)+min);
37
+ return Math.floor(Math.random()*(max - min +1)+min);
38
38
  }
39
39
 
40
40
  for (i = min; i <= max ; i++){
41
41
  while(true){
42
42
  let tmp = getRandom(min,max);
43
43
  if(!randoms.includes(tmp)){
44
- randoms.push(tmp);
44
+ randoms.push(tmp);
45
- break;
45
+ break;
46
46
  }
47
47
  }
48
48
  }

1

.

2019/11/15 05:17

投稿

azuapricot
azuapricot

スコア2343

answer CHANGED
@@ -1,4 +1,4 @@
1
- ```JavaScirpt
1
+ ```javaScirpt
2
2
  let randoms = [];
3
3
  let min = 1,max = 10;
4
4
 
@@ -23,4 +23,32 @@
23
23
  [ 8, 2, 7, 5, 3, 1, 6, 4, 10, 9 ]
24
24
  ```
25
25
 
26
- let → function に変えただけですが一応動くっぽいですね
26
+ let → function に変えただけですが一応動くっぽいですね
27
+
28
+ ---
29
+
30
+ 関数式を使いたいならこうでしょうか?
31
+
32
+ ```javaScript
33
+ let randoms = [];
34
+ let min = 1,max = 10;
35
+
36
+ let getRandom = function(min,max){
37
+ return Math.floor(Math.random()*(max - min +1)+min);
38
+ }
39
+
40
+ for (i = min; i <= max ; i++){
41
+ while(true){
42
+ let tmp = getRandom(min,max);
43
+ if(!randoms.includes(tmp)){
44
+ randoms.push(tmp);
45
+ break;
46
+ }
47
+ }
48
+ }
49
+ console.log(randoms);
50
+ ```
51
+ 出力結果
52
+ ```
53
+ [ 9, 10, 8, 2, 5, 7, 4, 1, 3, 6 ]
54
+ ```