We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fea064d commit ebe693fCopy full SHA for ebe693f
1 file changed
Candy-018/README.md
@@ -0,0 +1,21 @@
1
+## 編號:CANDY-018
2
+
3
+### 程式語言:JavaScript
4
5
+#### 題目:實作一個可以印出隨機整數的函數
6
7
+```js
8
+const randomNumber = function (min, ...max) {
9
+ return Math.floor(Math.random() * (max - min) + min);
10
+};
11
12
+// 帶入兩個參數 (min,...max)
13
+// 不確定幾個參數時使用其餘運算符
14
+// 其餘運算符沒有帶入值的時候在運算時會轉成空陣列 []
15
+// 接著轉成字串最後轉成 0
16
+// 隨機範圍需要最終值減去初始值後並加上初始值
17
+// 加上初始值是為了讓減去的結果範圍移動到初始值是最終值之間
18
19
+console.log(randomNumber(50)); // 隨機印出 0 ~ 49 之間的任何一個數字
20
+console.log(randomNumber(5, 30)); // 隨機印出 5 ~ 29 之間的任何一個數字
21
+```
0 commit comments