-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdemo.html
More file actions
42 lines (34 loc) · 959 Bytes
/
demo.html
File metadata and controls
42 lines (34 loc) · 959 Bytes
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="btn">随机数</button>
<script>
// 1. 给定的数据
const values = [200, 400, 600, 800, 1000]
// 2. 监听按钮的点击
const btnEl = document.querySelector(".btn")
btnEl.onclick = () => {
const currentValue = Math.floor(Math.random() * 1500)
matchIndex(currentValue)
}
// 匹配对应的索引
function matchIndex(currentValue) {
let currentIndex = values.length - 1
for (let i = 0; i < values.length; i++) {
const value = values[i]
if (value > currentValue) {
currentIndex = i - 1
break
}
}
if (currentIndex === -1) return
console.log("value:", currentValue, "index:", currentIndex)
}
</script>
</body>
</html>